So I have a class that has a composite primary key:
public class Field
{
[Key, Column(Order=0)]
[ForeignKey("Store")]
public int StoreID { get; set; }
public Store Store { get; set; }
[Key, Column(Order = 1)]
public int ID { get; set; }
public List<Template> Templates { get; set; }
}
…and another one that references it
public class Mapping
{
public Field Field { get; set; }
[ForeignKey("Template")]
public int TemplateID { get; set; }
public Template Template { get; set; }
}
This works well except that Mapping is missing its primary key, because I don’t know how to do it.
What I want is the class Mapping to have a composite primary key formed by Field and Template. The difficulty lies for me in that the Field class also has a composite key.
You would need something like this:
For the
ForeignKeyattribute you can specify a composite key order like for theKeyattribute. Alternatively you can also put theForeignKeyattribute on the navigation property and then specify the FK properties with comma separation:(Edit Duplicate
Columnattribute was wrong -> Corrected.)