title: Helping solving 'There is already an open DataReader associated with this Command which must be closed first' in ASP.NET Core 3.1 Application
body: <p>I need help fixing the error message:</p>
  
  <blockquote>
    <p>There is already an open DataReader associated with this Command which must be closed first.</p>
  </blockquote>
  
  <p>The problem is stemming from the line:</p>
  
  <pre><code>foreach (var user in _userManager.Users) {
  </code></pre>
  
  <p>I understand that basically, there is another reader open and the other hasn't been closed but I don't know how to fix this based on my code.  I searched around for similar problems and there are lots of them, some suggest adding no tracking, others to return <code>.ToList</code>.  I'm a bit lost with what to do about it and could use some help.</p>
  
  <p>Here is my code so far:</p>
  
  <p><strong>Controller</strong></p>
  
  <p>Both the role manager and the <code>UserManager</code> are injected, in this instance, I've extended the identity class using my own ApplicationUser.  It was my understanding that, upon injection, declaring items with <code>_</code> would apply no tracking, that might be wrong.</p>
  
  <pre><code>  private readonly RoleManager&lt;IdentityRole&gt; _roleManager;
    private readonly UserManager&lt;ApplicationUser&gt; _userManager;
  
    public AdministrationController(RoleManager&lt;IdentityRole&gt; roleManager,
           UserManager&lt;ApplicationUser&gt; userManager) 
    {
        _roleManager = roleManager;
        _userManager = userManager;
    }
  
  [HttpGet]
  public async Task&lt;IActionResult&gt; EditRole(string id)
  {
      var role = await _roleManager.FindByIdAsync(id);
  
      if (role == null) {
          ViewBag.ErrorMessage = $"Role with Id = {id} cannot be found";
          return View("NotFound");
      }
      var model = new EditRoleViewModel
      {
          Id = role.Id,
          RoleName = role.Name
      };
      foreach (var user in _userManager.Users) {
          if (await _userManager.IsInRoleAsync(user, role.Name))
          {
              model.Users.Add(user.UserName);
          }
       }
       return View(model);
   }
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <p>To avoid errors with the <code>@if (Model.Users.Any())</code> of the view, the <code>Users</code> collection is instantiated in the <code>ViewModel</code>.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.Linq;
  using System.Threading.Tasks;
  
  namespace WebApplication1
  {
      public class EditRoleViewModel
      {
          public EditRoleViewModel() {
              Users = new List&lt;string&gt;();
          }
          public string Id { get; set; }
          [Required(ErrorMessage = "Role name is required")]
          public string RoleName { get; set; }
          public List&lt;string&gt; Users { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>@model EditRoleViewModel
  @{ 
      ViewBag.Title = "Edit Role";
  }
  &lt;h1&gt;Edit Role&lt;/h1&gt;
  
  &lt;form method="post" class="mt-3"&gt;
      &lt;div class="form-group row"&gt;
          &lt;label asp-for="Id" class="col-sm-2 col-form-label"&gt;&lt;/label&gt;
          &lt;div class="col-sm-10"&gt;
              &lt;input asp-for="Id" disabled class="form-control" /&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group row"&gt;
          &lt;label asp-for="RoleName" class="col-sm-2 col-form-label"&gt;&lt;/label&gt;
          &lt;div class="col-sm-10"&gt;
              &lt;input asp-for="RoleName" class="form-control" /&gt;
              &lt;span asp-validation-for="RoleName" class="text-danger"&gt;&lt;/span&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div asp-validation-summary="All" class="text-danger"&gt;&lt;/div&gt;
  
      &lt;div class="form-group row"&gt;
          &lt;div class="col-sm-10"&gt;
              &lt;button type="submit" class="btn btn-parimary"&gt;Update&lt;/button&gt;
              &lt;a asp-action="ListRoles" class="btn btn-danger"&gt;Cancel&lt;/a&gt;
          &lt;/div&gt;
      &lt;/div&gt; 
  
      &lt;hr /&gt;
      &lt;div class="card"&gt;
          &lt;div class="card-header"&gt;
              &lt;h3&gt;Users in this role&lt;/h3&gt;
          &lt;/div&gt;
          &lt;div class="card-body"&gt;
              @if (Model.Users.Any())
              {
                  foreach (var user in Model.Users)
                  {
                      &lt;h5 class="card-title"&gt;@user&lt;/h5&gt;
                  }
              }
              else { 
                  &lt;h5 class="card-title"&gt;None at the moment&lt;/h5&gt;
              }
          &lt;/div&gt;
          &lt;div class="card-footer"&gt;
              &lt;a href="#" class="btn btn-primary" style="width: auto"&gt;Add Users&lt;/a&gt;
              &lt;a href="#" class="btn btn-primary" style="width: auto"&gt;Remove Users&lt;/a&gt;
          &lt;/div&gt;
      &lt;/div&gt;    
  &lt;/form&gt;
  </code></pre>
  
  <p>Any help with this is appreciated.</p>