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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With