There is a database that contains a list of users created using Identity. Each user belongs to a specific role. Roles are in a different table, and, therefore, is linked through a different table.
How to get a list of users from a specific role by referring to this table, where is the user ID and the ID of his role? I can't imagine how to make a request, because I have never worked with EF ...

  • And can you somehow clarify which table is this and which one? - qzavyer
  • @qzavyer, when initializing the database, it created the tables AspNetUsers, AspNetRoles, AspNetUserRoles - a typical many-to-many relationship. And here I need to get from AspUsers users who have a specific role. I mean, as I imagine, take the name of the role, find its Id, from the table AspNetUserRoles take the Id of users for this role and display information about them from AspNetUsers - guitarhero

1 answer 1

var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(context)); var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)); string roleName = "Admin"; var role = await roleManager.Roles.SingleAsync(r => r.Name == roleName); var users = userManager.Users.Where(u => u.Roles.Any(r => r.RoleId == role.Id));