1

I want to add intersection table in EF Code First and all the time I get information that - Column names in each table must be unique. Column name 'Product_Unit_Product_UnitID' in table 'PRODUCTs' is specified more than once.

This is code:

public class PRODUCT
{
    public int ProductID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price_Net { get; set; }
    public decimal Price_Gross { get; set; }
    public int Vat { get; set; }
    public decimal Price_Offer_Net { get; set; }
    public decimal Price_Offer_Gross { get; set; }
    public bool Is_Offer { get; set; }
    public bool Is_Lock { get; set; }
    public OFFER Offer { get; set; }
    public ICollection<PRICE_CUSTOMER> Price_Customer { get; set; }
    public int CategoryID { get; set; }
    public virtual CATEGORY Category { get; set; }
    public int BrandID { get; set; }
    public virtual BRAND Brand { get; set; }
    public int StockID { get; set; }
    public virtual STOCK Stock { get; set; }
    public ICollection<PRODUCT_UNIT> Product_Unit_L { get; set; }
    public int Product_UnitID { get; set; }
    public virtual PRODUCT_UNIT Product_Unit { get; set; }

}

and PRODUCT_UNIT

public class PRODUCT_UNIT
{
    public int Product_UnitID { get; set; }
    public decimal Converter { get; set; }
    public int Barcode { get; set; }
    public ICollection<PRODUCT> Product_L { get; set; }
    public int ProductID { get; set; }
    public virtual PRODUCT Product { get; set; }
    public int UnitID { get; set; }
    public virtual UNIT Unit { get; set; }

}

So how to add intersection table in the code first? Thanks for all help

enter image description here

cyprian
  • 497
  • 1
  • 6
  • 21

1 Answers1

0

You can specify the column names of the generated tables (and the table name) using data annotation attributes. You might also want to specify the primary key column explicitly using the key attribute.

[Table("MyTable")]
pulic class MyClass
{
  [Key]
  [Column("MyIdColumn")]
  public int Id { get; set; }
}

But I would first try to rename the the primary property (column) of the intersection table to from Product_UnitID to Id. This might also fix the problem.


Automatic primary key detection

If you do not specify the primary key explictly (e.g. using the key attribute or using fluent API), EF will look for a column named Id or TableNameId as explained here: Default id naming convention in Entity framework code first

Community
  • 1
  • 1
Martin
  • 5,165
  • 1
  • 37
  • 50