Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Composite PK without key property

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.

like image 703
Alex G.P. Avatar asked Nov 25 '25 18:11

Alex G.P.


1 Answers

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 int or string or Guid, etc.

like image 137
Slauma Avatar answered Nov 28 '25 15:11

Slauma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!