title: Checking if a user is in a role in asp.net mvc Identity
body: <p>I'm having an issue seeding my database with users and roles.</p>
  
  <p>The User and the Role are both created (I can see them in the database after the error is thrown).</p>
  
  <p>However, when I try to check if the user is in a role, I get an exception.</p>
  
  <p>My code is:</p>
  
  <pre><code>    public class tbInitializer&lt;T&gt; : DropCreateDatabaseAlways&lt;tbContext&gt;
      {
      protected override void Seed(tbContext context)
      {
          ApplicationDbContext userscontext = new ApplicationDbContext();
          var userStore = new UserStore&lt;ApplicationUser&gt;(userscontext);
          var userManager = new UserManager&lt;ApplicationUser&gt;(userStore);
  
          var roleStore = new RoleStore&lt;IdentityRole&gt;(userscontext);
          var roleManager = new RoleManager&lt;IdentityRole&gt;(roleStore);
  
  
          if(!userscontext.Users.Any(x=&gt; x.UserName=="marktest"))
          {
              var user = new ApplicationUser { UserName = "marktest", Email = "marktest@gmail.com" };
              userManager.Create(user, "Pa$$W0rD!");
          }
  
          if (!roleManager.RoleExists("Admin"))
          { 
              roleManager.Create(new IdentityRole("Admin"));
          }
  
          if(!userManager.IsInRole("marktest","Admin"))
          { 
              userManager.AddToRole("marktest","Admin");
          }
  </code></pre>
  
  <p>However, on the line:</p>
  
  <p><code>if(!userManager.IsInRole("marktest","Admin"))</code></p>
  
  <p>An exception is thrown with the error: <code>UserId not found.</code></p>
  
  <p>The User and the Role are both in the database when I check after the exception is thrown:</p>
  
  <p><img src="https://i.stack.imgur.com/COW3X.png" alt="Shows User Added to DB"></p>
  
  <p><img src="https://i.stack.imgur.com/IOgs9.png" alt="Shows Role Added to DB"></p>
  
  <p>Can anyone see what I'm doing wrong?</p>
  
  <p>Thanks for any help,</p>
  
  <p>Mark</p>