简体   繁体   中英

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

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. 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

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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