I have 3 tables –
User (Id, Name)
Roles (Id, Name)
UserRoles (UserId, RoleId)
I think they are self explanatory. How do I update an entry (UserId and RoleId) in UserRoles?
context.User.Roles gives me the list of roles but how do I update them?
Thank you.
From your comment:
First of all, you should NOT update the Id’s.
Secondly, since you are using EF, you should try to think in terms of objects (or entities), rather than “DB-many-to-many-mapping-tables”. Every
Userentity has a collection ofRoles. If you remove aRolefrom theUser.Rolescollection and callcontext.SaveChanges(), the corresponding entry will be deleted from theUserRolestabele. Similarly, when you add aRoleobject to theUser.Rolescollection, and save changes, a new entry will be created in theUserRolestable.The following sample might be useful for clarity:
(null-reference checks omitted for simplicity).