EF4 How to expose the join table in a many to many relationship

后端 未结 2 703
天涯浪人
天涯浪人 2021-02-06 17:35

Say I have the following tables:

Essence, EssenceSet, and Essence2EssenceSet where Essence2EssenceSet holds just the IDs of the 1st 2 tables to form the M:M relationship

2条回答
  •  萌比男神i
    2021-02-06 18:14

    Entity Framework is an ORM, and as such, when you work with it you aren't suppose to think of the database in terms of tables but instead in terms of objects. You shouldn't be inserting the identity into a table that holds the M2M relationship, but you should be loading one side of the relationship, which should expose a collection of the other side and add it to that collection. For a M2M, you may need to load the other side and do the same.

    Also, I believe EF prefers all tables to have a single column PK (I could be wrong on this), but you may need to add a column to the M2M and designate it as a PK.

提交回复
热议问题