[英]Foreign key C# MVC 5?
我想將字符串字段關聯為外鍵,請給我幫助!
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; }
}
您好,您需要在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);
}
}
請參考以下類結構。 為了使“ 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的答案是正確的,但只要完成,您還可以使用數據注釋:
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.