In 2.2 I could write something like this:
List<Claim> claims = new List<Claim>();
var userRoles = await _userManager.GetRolesAsync(user);
foreach (var role in _roleManager.Roles.Where(a => userRoles.Contains(a.Name)))
{
claims.AddRange(await _roleManager.GetClaimsAsync(role));
}
return claims;
In 3.1 it gives me this error:
System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.
But if I add
ToList()
to the forEach clause, it works fine(like this):
List<Claim> claims = new List<Claim>();
var userRoles = await _userManager.GetRolesAsync(user);
foreach (var role in _roleManager.Roles.Where(a => userRoles.Contains(a.Name)).ToList())
{
claims.AddRange(await _roleManager.GetClaimsAsync(role));
}
return claims;
Should I change all places in my code where I used similar construct, or there is a way to make EF work normal with it?