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


1 answer


Not a real answer, but last time it happens to me, I have a wrong mapping like HasOptional(xx).WithMany();

(missing .HasForeignKey(xx)

) and the EF conventions are in another column xx_Id

- maybe you should check this.



0


source







All Articles