title: userManager.AddToRoleAsync() - Error: role does not exist
body: <p>I'm creating a user registration system using .NET Core, Identity Core, and MVC Core. I'm able to create users and create roles in the database.</p>
  
  <p>Here's the form on the view that lets me select a user and select a role to add:</p>
  
  <pre><code>@using (Html.BeginForm("AddRoleToUser", "Roles"))
  {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary(true)
  
      &lt;p&gt;
          Username : @Html.DropDownList("UserName", (IEnumerable&lt;SelectListItem&gt;)ViewBag.Users, "Select ...")
          Role Name: @Html.DropDownList("RoleName", (IEnumerable&lt;SelectListItem&gt;)ViewBag.Roles, "Select ...")
  
      &lt;/p&gt;
  
      &lt;input type="submit" value="Save" /&gt;
  }
  </code></pre>
  
  <p>These drop-down lists are populated with users and roles that already exist in the database. They allow me to select <code>User</code>s , and the <strong>name</strong> of a role that I've already created. For example, I have a role with the name "admin", this form lets me select the string "admin".</p>
  
  <p>Here's the action that handles adding a role to a user:</p>
  
  <pre><code>    [HttpPost]
      [ValidateAntiForgeryToken]
      public async Task&lt;IActionResult&gt; AddRoleToUser(string UserName, string RoleName)
      {
          try
          {
              ApplicationUser user = _db.Users.Where(u =&gt; u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
              await _userManager.AddToRoleAsync(user, RoleName);
              PrepopulateDropDownMenus();
              ViewBag.ResultMessage = "Role created successfully!";
              return View("Manage", "Roles");
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex);
              return View("Manage");
          }
      }
  </code></pre>
  
  <p>The action never adds the role to the user, and the exception reads "Role "ADMIN" does not exist." with no inner exception. I've tried turning the <code>RoleName</code> in the action parameters to all-caps, but it still does not find the role. I've also tried using the role ID instead of the name, which was also unsuccessful.</p>
  
  <p>This exact code worked when I built this app using Identity 3.0 with MVC 6. It seems like something has changed in moving over to Identity Core.</p>
  
  <p>Any thoughts?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Here's the code I'm using to populate the drop-down lists in <code>RolesController</code> via the Viewbag:</p>
  
  <pre><code>    private void PrepopulateDropDownMenus()
      {
          var rolesList = _db.Roles.OrderBy(r =&gt; r.Name).ToList().Select(rr =&gt; new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
          var usersList = _db.Users.OrderBy(u =&gt; u.UserName).ToList().Select(uu =&gt; new SelectListItem { Value = uu.UserName.ToString(), Text = uu.UserName }).ToList();
          ViewBag.Roles = rolesList;
          ViewBag.Users = usersList;
      }
  </code></pre>
  
  <p>Here's how I add Identity in <em>Startup.cs</em> in the <code>ConfigureServices</code> method:</p>
  
  <pre><code>    public void ConfigureServices(IServiceCollection services)
      {
          ...
          services.AddEntityFramework()
              .AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
                  options.UseSqlServer(Configuration["ConnectionStrings:DefaultConnection"]));
          services.AddIdentity&lt;ApplicationUser, IdentityRole&gt;()
              .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;()
              .AddDefaultTokenProviders();
      }
  </code></pre>
  
  <p>Here's the route in <em>RolesController.cs</em> I use to create a new role in the database:</p>
  
  <pre><code>    [HttpPost]
      public IActionResult Create(string rolename)
      {
              _db.Roles.Add(new IdentityRole()
              {
                  Name = rolename
              });
              _db.SaveChanges();
              ViewBag.ResultMessage = "Role created successfully!";
              return RedirectToAction("Index");
      }
  </code></pre>