AspNetUserRoles Table is not populated using Entity Framework Database First in MVC 5

asp.net-mvc asp.net-mvc-5 entity-framework

Question

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.

image

Can anyone give me some hints on this? Many thanks.

1
6
9/8/2017 6:44:54 AM

Popular Answer

If you want to manage the AspNetUsersRoles, you must make the following modification.

  1. Remove the primary key, actually is conformed by UserId and RoleId

  2. Add a new Id field in the table and check like primary key.

  3. Update your model.

enter image description here

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!

7
9/3/2018 3:23:46 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow