[英]EF 5 Code First Extra Foreign Keys being Generated
Occasionally EF would include a *_Id foreign key to the generated tables, even though I specify the foreign key. 偶尔EF会在生成的表中包含* _Id外键,即使我指定了外键。
Classes: 类别:
public partial class Product : IFoo, IBar
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[XmlIgnore, ScriptIgnore, ForeignKey("InsuranceClassId"), Display(Name = "Insurance Class")]
public virtual InsuranceClass InsuranceClass { get; set; }
[Display(Name = "Insurance Class")]
public int InsuranceClassId { get; set; }
[XmlIgnore, ScriptIgnore, ForeignKey("ProductGroupId"), Display(Name = "Product Group")]
public virtual ProductGroup ProductGroup { get; set; }
[Display(Name = "Product Group")]
public int ProductGroupId { get; set; }
//[Snip]
}
public partial class InsuranceClass : IFoo, IBar
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[MaxLength(255), Display(Name = "Description"), Required(AllowEmptyStrings = false)]
public string Description { get; set; }
[XmlIgnore, ScriptIgnore]
public virtual List<Product> Products { get; set; }
public InsuranceClass()
{
this.Products = new List<Product>();
}
//[Snip]
}
But my Products table end up like this: 但我的Products表最终如下:
Products
Id
InsuranceClassId
ProductGroupId
InsuranceClass_Id
ProductGroup_Id
What am I doing wrong? 我究竟做错了什么? It only happens on a few tables.
它只发生在几张桌子上。
Not real answer, but last time this happens to me, i have wrong mapping, like HasOptional(xx).WithMany();
不是真正的答案,但上次这发生在我身上,我有错误的映射,如
HasOptional(xx).WithMany();
(missing .HasForeignKey(xx)
) and EF Conventions kick in another xx_Id
column - maybe you should check this. (缺少
.HasForeignKey(xx)
)和EF约定会xx_Id
另一个xx_Id
列 - 也许你应该检查一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.