In this blogpost I want to show you a way to realize code First with Entity Framework n:m relationship with additional information.
a few days ago I faced the problem of having a normal N:M Relationship in EF with additional information in the table which keeps the two entities together.
Well, without having these additional information this is easy:
EF is now going to make the right decisions for you while creating the database. A third table is created (due to EF-magic) and shows you the right relation-table. Great things so far. But what if you want to have more information on the relation table which EF created for you? Well, the answer ist easy: EF is not able to do this without your help.
You have to create a third entity representing the relationship you want. I will now show how and I will show the right Fluent-Configuration to map the Keys etc. in a correct way. (Entities should not know what their fields are used for. So things like [Key], … have to be avoided! This is why you have Fluent-API!)
So first, please create your third entity:
and extend your existing entities like the following:
Right now, you have made the three entities. Now, we have to wire everything together:
When you now run your application with the right code-first configuration your database should hold those 3 three tables.
Note: Now you have to think exactly about what you want to do (Well you should do this always while coding 😉 ). Adding a new group has to get another entry in the Group-Table. but adding or deleting users are only reached by editing the Groups2Users-Table. (Perhaps you should spend this table an own repository 😉 ).
When you for example want to have all Groups of a user just call:
Adding a new group would be like
Hope this helps,