简体   繁体   English

外键C#MVC 5?

[英]Foreign key C# MVC 5?

I would like to relate a string field as a foreign key, I need help please! 我想将字符串字段关联为外键,请给我帮助!

catalogtype.code => catalog.typeCatalogRefCode 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; }
}

Hi for this you need to write code on override method OnModelCreating of your dbcontext class. 您好,您需要在dbcontext类的重写方法OnModelCreating上编写代码。 Please refer the following code. 请参考以下代码。

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);
    }
}

Please refer the following class structure. 请参考以下类结构。 For making "typeCatalogRefCode" as foreign key you need to define "code" as key 为了使“ 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; }
}

Koderzzzz answer is right but just to complete you can also use data annotation : 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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM