I'm playing around with an ASP Core Identity project.
For the admin section, to add users to roles I want a list of current members of that role and a list of non-members. Then I can move users between them using checkboxes or arrow buttons.
My code gets the error:
**InvalidOperationException: There is already an open DataReader associated with this Connection which must be closed first.**
It works when I enable MARS in the database connection string (I'm using entity):
MultipleActiveResultSets=True
My code:
private RoleManager<IdentityRole> roleManager;
private UserManager<User> userManager;
public RoleController(RoleManager<IdentityRole> roleMgr, UserManager<User> userMrg)
{
// Constructor
// Role Manager, UserManager Dependancy Injection
roleManager = roleMgr;
userManager = userMrg;
}
public async Task<IActionResult> Update(string id)
{
IdentityRole role = await roleManager.FindByIdAsync(id);
List<User> members = new List<User>();
List<User> nonMembers = new List<User>();
foreach (User user in userManager.Users)
{
// We need MARS enabled in connection string
// MultipleActiveResultSets=True
// We are creating a list of a list and not closing the data reader?
var list = await userManager.IsInRoleAsync(user, role.Name) ? members : nonMembers;
list.Add(user);
}
return View(new RoleEdit
{
Role = role,
Members = members,
NonMembers = nonMembers
});
}
So you would send an id to the Update() module
mysite.com/controller/update/1
and see a list like this:
How can I rework this so I don't have to use MARS ? I'm told it is a workaround and not a solution to use it.
Any help is appreciated.