1

I am a novice. Take it easy on me.
this is code for my action

[HttpGet]
    public async  Task<IActionResult> EditRole(string id)
    {
        var role = await roleManager.FindByIdAsync(id);
        if(role == null)
        {
            ViewBag.ErrorMessage = $"Role with Id = {id} cant be found";
            return View("NotFound");
        }

        var model = new EditRoleViewModel()
        {
            Id = role.Id,
            RoleName = role.Name
        };

        foreach(var user in userManager.Users)
        {
            try
            {
                if (await userManager.IsInRoleAsync(user, role.Name))
                {
                    model.Users.Add(user.UserName);
                }
            }
            catch (Exception ex)
            {

                throw;
            }
            
        }

        return View(model);
    }

and when check my user is in this role throw this exception

{"There is already an open DataReader associated with this Connection which must be closed first."}

enter image description here

8
  • So what does roleManager.FindByIdAsync or userManager.IsInRoleAsync do?
    – nvoigt
    May 21, 2021 at 14:50
  • RoleManager<IdentityRole> roleManager; UserManager<ApplicationUser> userManager; these are @nvoigt May 21, 2021 at 14:52
  • Which line threw the exception? It probably has a StackTrace that will tell you.
    – nvoigt
    May 21, 2021 at 14:54
  • I think you need to call SaveChangesAsync() after every Add() or perform an AddRange() and then call SaveChangesAsync() May 21, 2021 at 14:55
  • if (await userManager.IsInRoleAsync(user, role.Name)) on this line throw exception @nvoigt May 21, 2021 at 14:56

1 Answer 1

2

Change from this:

 foreach(var user in userManager.Users)
        {
            try
            {
                if (await userManager.IsInRoleAsync(user, role.Name))
                {
                    model.Users.Add(user.UserName);
                }
            }
            catch (Exception ex)
            {

                throw;
            }
            
        }

To this:

var users = userManager.Users.ToList();

 foreach(var user in users)
        {
            try
            {
                if (await userManager.IsInRoleAsync(user, role.Name))
                {
                    model.Users.Add(user.UserName);
                }
            }
            catch (Exception ex)
            {

                throw;
            }
            
        }

Putting the userManager.Users on the foreach loop creates a connection to retrieve the users from the database and it is not closed until finished, then when you call userManager.IsInRoleAsync there is already a connection open in the db.

2
  • Another option is maybe call userManager.Users.ToList() to force finish the users consult on the foreach May 21, 2021 at 15:23
  • thanks this worked I forget this is open until to convert and execute by tolist() May 21, 2021 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.