我已经阅读了十篇关于这个问题的帖子,但仍然无法理解我的失败原因:我被困在这两天以后。
ASPNET MVC5网络应用程序。代码优先
型号:
public partial class Category
{
public int ID { get; set; }
public string Name { get; set; }
public int? ParentID { get; set; }
public bool IsDeleted { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
public class ISO_Languages
{
public int ID { get; set; }
public string code { get; set; }
public bool IsEnabled { get; set; }
public string name_en { get; set; }
public string name_fr { get; set; }
public string name_it { get; set; }
public string name_de { get; set; }
public string name_es { get; set; }
}
public class CategoryTrans
{
[Key]
[Column(Order = 1)]
public int category_id { get; set; }
[Key]
[Column(Order = 2)]
public int language_id { get; set; }
public string name { get; set; }
}
CategoryTrans具有基于两个FK的复合PK:类别ID和ISO_Languages ID
我只是想在CategoryTrans模型中正确引用这个架构,就像:
public class CategoryTrans
{
[Key, Column(Order = 1)]
public int category_id { get; set; }
[Key, Column(Order = 2)]
public int language_id { get; set; }
[ForeignKey("ID")]
public virtual Category ID{ get; set; }
[ForeignKey("ID")]
public virtual ISO_Languages ID2{ get; set; }
public string name { get; set; }
属性&#39; ID&#39;上的ForeignKeyAttribute在类型&#39; xyz.Models.CategoryTrans&#39;无效。外键名称&#39; ID&#39;在依赖类型&#39; xyz.Models.CategoryTrans&#39;中找不到。 Name值应为逗号分隔的外键属性名称列表
public class CategoryTrans
{
[Key, Column(Order = 1)]
public int category_id { get; set; }
[Key, Column(Order = 2)]
public int language_id { get; set; }
[ForeignKey("Category")]
public virtual Category catid{ get; set; }
[ForeignKey("ISO_Languages")]
public virtual ISO_Languages languguageid{ get; set; }
public string name { get; set; }
属性&#39; catid&#39;上的ForeignKeyAttribute在类型&#39; xyz.Models.CategoryTrans&#39;无效。外键名称&#39;类别&#39;在依赖类型&#39; xyz.Models.CategoryTrans&#39;中找不到。 Name值应该是以逗号分隔的外键属性名称列表。
我想做一些不可能的事吗?
答案 0 :(得分:1)
尝试:
public partial class Category
{
public int ID { get; set; }
public string Name { get; set; }
public int? ParentID { get; set; }
public bool IsDeleted { get; set; }
public virtual ICollection<Product> Products { get; set; }
public virtual ICollection<CategoryTrans> CategoryTrans { get; set; }
}
public class ISO_Languages
{
public int ID { get; set; }
public string code { get; set; }
public bool IsEnabled { get; set; }
public string name_en { get; set; }
public string name_fr { get; set; }
public string name_it { get; set; }
public string name_de { get; set; }
public string name_es { get; set; }
public virtual ICollection<CategoryTrans> CategoryTrans { get; set; }
}
public class CategoryTrans
{
[Key, Column(Order = 1)]
public int category_id { get; set; }
[Key, Column(Order = 2)]
public int language_id { get; set; }
[ForeignKey("category_id")]
public virtual Category ID{ get; set; }
[ForeignKey("language_id")]
public virtual ISO_Languages ID2{ get; set; }
public string name { get; set; }
}