CzarnePaski CzarnePaski - 1 year ago 151
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 Source

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; }

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download