In my project I am trying to retrieve all the users and their roles in MVC 5 using Entity Framework.
The DBContext is set up using Database first. But it is wearied that the AspNetUserRoles Table is not populated automatically and the relationship between AspNetUser and AspNetRole is many-to-many.
Below is the link to the screenshot.
Can anyone give me some hints on this? Many thanks.
If you want to manage the AspNetUsersRoles, you must make the following modification.
Remove the primary key, actually is conformed by UserId and RoleId
Add a new Id field in the table and check like primary key.
Update your model.
Now you can scaffolding this entity and create the controller and the view for manage this.
Note: Remember that by changing the primary key of this table on the DB side means you have to control how AspNetUserRoles records are created on the Application Side, considering you now could have record redundancy on the table, for instance: MyRoles=1 (PK), UserID=1, RoleID=1, Comment= User1 is Admin MyRoles=2 (PK), UserID=1, RoleID=2, Comment= User1 is Admin again
Which means the same, so manage this logic upon AspNetUserRoles creation!