Using Entity Framework 4 and code first how would I create a model that supports this scenario:
In an application, there are users, each user belongs to one or more groups and for each group the user can have one or more roles.
Example:
I would like to be able to say, “give me Lisa”, and the response returns a user object for lisa, with the groups she belongs to. For each group there is a list property with all the roles she has for that particular group
Can anyone help me model this using code first, any help/code samples, would be great!
/Best regards Vinblad
Edit: Here is new model for your requirement.
As described in code there is small disadvantage. You can avoid the disadvantage by enforcing data integrity in DB by additional FK which can’t be modeled by code first. You can use custom initializer to add that FK:
Just add this to your application initialization (only for debug – application should not be able to drop its database in release):