I using manual mapping entity in my Code First on EF5 project and would like to use FKs as composite PK. I have only navigation properties in mapping class (not FK) as mentioned below:
class MyMapping
{
public virtual Mapped1 {get;set;}
public virtual Mapped2 {get;set;}
}
I do not want to add fields like ‘int Mapped1FK’/’int Mapped2FK’, but I cannot find how to specify composite key in this case.
I assume that it should be something like:
modelBuilder.Entity<MyMapping>()
.HasKey(k => k.Mapped1)
.HasKey(k => k.Mapped2);
or
modelBuilder.Entity<MyMapping>()
.HasKey(k => { k.Mapped1, k.Mapped2} );
but I do not know what exactly right.
It’s not possible to use navigation properties as primary key properties (no matter if composite or not). Key properties must have a primitive type – like
intorstringorGuid, etc.