one-to-many relation using two columns in Entity Framework Core

前端 未结 2 4589
花落未央
花落未央 2021-02-20 19:04

In my project I have a table Translation that can have translations for any model. To achieve this, the table has two fields: Model and ModelId

2条回答
  •  长发绾君心
    2021-02-20 19:21

    Yes, there is. Use this:

    modelBuilder.Entity()
    .HasMany(bm => bm.Translations)
    .WithForeignKey(x => new { x.Key1, x.Key2 });
    

    Key1 and Key2 obviously need to be the keys to the relation and you may also need to define them as such (in the same order) for the Translation entity.

提交回复
热议问题