I use Identity 2 in my project and I Want display the list of AspNetUserRoles in the view. but I can not access to this table from DB object.how to access this table in ASP.NET MVC 5 !? Thanks in advance for your help
This will give you a list of all users and their roles.
Give your context name
using(var context = new YourContextName())
{
var usersAndRoles = new List<UserRoleModel>(); // Adding this model just to have it in a nice list.
var users = context.AspNetUsers;
foreach(var user in users)
{
foreach(var role in user.Roles)
{
usersAndRoles.Add(new UserRoleModel
{
UserName = user.UserName,
RoleName = role.Name
});
}
}
}
I haven't tested the code, there can be a more optimal way.