【问题标题】:Foreign key C# MVC 5?外键 C# MVC 5?
【发布时间】:2017-07-27 12:44:02
【问题描述】:

我想将一个字符串字段关联为外键,我需要帮助!

catalogtype.code => catalog.typeCatalogRefCode

public class CatalogType : AuditFields
{
    [Key]
    public int id { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public string code { get; set; }
    public bool passive { get; set; }
    public ICollection<Catalog> Catalogs { get; set; }

}

public class Catalog : AuditFields
{
    [Key]
    public int id { get; set; }
    public string value { get; set; }
    public string description { get; set; }
    public string code { get; set; }
    public bool passive { get; set; }
    public int order { get; set; }
    public string typeCatalogRefCode { get; set; }
    public virtual CatalogType catalogType { get; set; }
}

.................................................. …………

public class AuditFields
{
    public DateTime createdDate { get; set; }
    public string createdBy { get; set; }
    public string createdIn { get; set; }
    public DateTime modifiedDate { get; set; }
    public string modifiedBy { get; set; }
    public string modifiedIn { get; set; }
}

【问题讨论】:

    标签: c# asp.net-mvc-5 foreign-keys


    【解决方案1】:

    请参考以下类结构。要将“typeCatalogRefCode”作为外键,您需要将“code”定义为键

    class MyDBContext:DbContext
    {
        public MyDBContext()
            : base("mydbcontext")
        {
    
        }
        static MyDBContext()
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyDBContext>());
    }
    
    public DbSet<CatalogType> CatalogTypes{ get; set; }
    public DbSet<Catalog> Catalogs{ get; set; }
    }
    public class CatalogType : AuditFields
    {
    
        public int id { get; set; }
        public string name { get; set; }
        public string description { get; set; }
                [Key]
        public string code { get; set; }
        public bool passive { get; set; }
        public ICollection<Catalog> Catalogs { get; set; }
    
    }
    
    public class Catalog : AuditFields
    {
        [Key]
        public int id { get; set; }
        public string value { get; set; }
        public string description { get; set; }
        public string code { get; set; }
        public bool passive { get; set; }
        public int order { get; set; }
        public string typeCatalogRefCode { get; set; }
          [ForeignKey("typeCatalogRefCode")]
        public virtual CatalogType catalogType { get; set; }
    }
    public class AuditFields
    {
        public DateTime createdDate { get; set; }
        public string createdBy { get; set; }
        public string createdIn { get; set; }
        public DateTime modifiedDate { get; set; }
        public string modifiedBy { get; set; }
        public string modifiedIn { get; set; }
    }
    

    【讨论】:

    • 谢谢,除了通过将代码更改为主键来改变表的结构之外,我没有找到其他方法。甚至带有注释索引 [IsUnique = true] 的代码也不起作用。
    【解决方案2】:

    您好,您需要在 dbcontext 类的覆盖方法 OnModelCreating 上编写代码。请参考以下代码。

    public class CatalogContext : DbContext
    {
        static CatalogContext()
        {
            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<CatalogContext>());
        }
        public DbSet<CatalogType> CatalogTypes{ get; set; }
        public DbSet<Catalog> Catalogs{ get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelbuilder.Entity<Catalog>().HasOptional(a=>a.catalogType)
                                .WithMany(au=>au.Catalogs)
                                .HasForeignKey(a=>typeCatalogRefCode);
        }
    }
    

    【讨论】:

    • 投票。您还可以添加通过数据注释实现它的方式
    • 生成迁移时出错:Catalog_catalogType_Target_Catalog_catalogType_Source: : 引用约束的从属角色中所有属性的类型必须与主体角色中对应的属性类型相同。实体“Catalog”上的属性“typeCatalogRefCode”类型与引用约束“Catalog_catalogType”中实体“CatalogType”上的属性“id”类型不匹配。
    • 您的 AuditFields 课程是什么,请分享... @Hilarioc
    • 我分享了 AuditFild 类。
    【解决方案3】:

    Koderzzzz 的答案是正确的,但只是为了完成你也可以使用数据注释:

    public class CatalogType : AuditFields
    {
        [Key]
        public int id { get; set; }
        public string name { get; set; }
        public string description { get; set; }
        public string code { get; set; }
        public bool passive { get; set; }
        [InverseProperty("catalogType")]
        public ICollection<Catalog> Catalogs { get; set; }
    
    }
    
    public class Catalog : AuditFields
    {
        [Key]
        public int id { get; set; }
        public string value { get; set; }
        public string description { get; set; }
        public string code { get; set; }
        public bool passive { get; set; }
        public int order { get; set; }
        public string typeCatalogRefCode { get; set; }
        [ForeignKey("typeCatalogRefCode")]
        public virtual CatalogType catalogType { get; set; }
    }
    

    【讨论】:

    • 我对注解也是这样: CatalogType_Catalogs_Source_CatalogType_Catalogs_Target: : 引用约束的Dependent Role中所有属性的类型必须与Principal Role中对应的属性类型相同。实体“Catalog”上的属性“typeCatalogRefCode”类型与引用约束“CatalogType_Catalogs”中实体“CatalogType”上的属性“id”类型不匹配。
    猜你喜欢
    • 2014-04-04
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    • 2017-05-10
    • 2014-02-23
    • 1970-01-01
    相关资源
    最近更新 更多