Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't navigate to role name from IdentityUserRole in Identity v2

Prior to ASP Identity v2, you could do this..

var context = new ApplicationDbContext();

var role = context.Users.FirstOrDefault().Roles.Single(p => p.Role.Name == "Some role name");

As soon as I upgraded to Identity v2, lines like this in my application stopped compiling. There is no longer a Role property on IdentityUserRole, so it's not easy to, for example, find all users in the "Administrators" role.

I know there are other methods of finding users in a role, but my needs are different. This is a simplified example, but I need to construct a specific list of users with specific role information, sorta like this:

var data = context.Users.Select(p => new
{
    Administrator = p.Roles.Any(role => role.Role.Name == "Administrator"),
    ServiceProvider = p.Roles.Any(role => role.Role.Name == "ServiceProvider"),
    Lender = p.Roles.Any(role => role.Role.Name == "Lender"),
    Inspector = p.Roles.Any(role => role.Role.Name == "Inspector")
});

It doesn't appear as if that kind of query is possible any more with Identity v2.

like image 513
Alex Dresko Avatar asked Mar 30 '14 14:03

Alex Dresko


1 Answers

We will likely be adding a new helper method on the role manager to get all users in a role. We couldn't keep navigation properties on both User and Role because that would have resulted in a circular reference of two generic types. This should be added in version 2.1.

like image 177
Hao Kung Avatar answered Jan 02 '23 12:01

Hao Kung