0

i have some issue with making primary key from 2 foregin keys

model examples: Element, Color, ElementToColor

ElementToColor should contain 2 foregin keys that representing the Element and Color relation

[Key,Column(Order = 1)]
public Int ElementId {get; set;} 

[Key,Column(Order = 2)]
public Int ColorId {get; set;} 

but when i tring to add the migration it's faild

ElementToColor must have primary key

but it's wrong to make primary key for that king of mapping table, it will make dublicate data

1,1,1

2,1,1

...

what should i do in that case

I use:

application: asp.net core 2.1 webApi

database: MySql

ORM: MySql EFCore

Leon Barkan
  • 2,676
  • 2
  • 19
  • 43

1 Answers1

0

found it, i needed to use fluent API:

in the ApplicationDbContext i should override method OnModelCreating like that:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ElementToColor>().HasKey(e => new { e.ElementId, e.ColorId }
    );
Leon Barkan
  • 2,676
  • 2
  • 19
  • 43