CzarnePaski CzarnePaski - 3 days ago 5
C# Question

Table connecting other tables. Entity Framework - error 6002: no primary key

I have got table connecting other tables. Entity Framework wants a primary key. I'd like this table have only ID from other tables without additional primary key. What should I do to fix this error?

Answer

You can create a primary composite key

public class YourEntity
{
 [Column(Order = 0), Key, ForeignKey("FieldFromAnotherTable_1")]
 public int FieldFromAnotherTable_1_ID { get; set; }

 [Column(Order = 1), Key, ForeignKey("FieldFromAnotherTable_2")]
 public int FieldFromAnotherTable_2_ID { get; set; }

 public virtual Type1 FieldFromAnotherTable_1{ get; set; }
 public virtual Type2 FieldFromAnotherTable_2{ get; set; }

}
Comments