EF 5 Code First additional foreign keys are generated
Sometimes EF included the * _Id foreign key in the generated tables even though I am specifying the foreign key.
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 the My Products table ends up like this:
Products
Id
InsuranceClassId
ProductGroupId
InsuranceClass_Id
ProductGroup_Id
What am I doing wrong? This only happens on a few tables.
+3
source to share