title: How to Seed Users and Roles with Code First Migration using OWIN Identity 2 with MVC5
body: <p>I am trying to seed users and roles with Code First Migration and OWIN Identity 2 with MVC5. I had this working before upgrading to 2.0.</p>
  
  <p>First, the following works when using the non code first migration example for <a href="http://www.codeproject.com/Articles/762428/ASP-NET-MVC-and-Identity-Understanding-the-Basics" rel="nofollow noreferrer">DropCreateDatabaseIfModelChanges</a> </p>
  
  <p>The ApplicationDbInitializer Class from the IdentityConfig.cs File:</p>
  
  <pre><code>public class ApplicationDbInitializer : DropCreateDatabaseIfModelChanges&lt;ApplicationDbContext&gt; 
  {
      protected override void Seed(ApplicationDbContext context) 
      {
          InitializeIdentityForEF(context);
          base.Seed(context);
      }
  
  
      public static void InitializeIdentityForEF(ApplicationDbContext db) 
      {
          var userManager = HttpContext
              .Current.GetOwinContext()
              .GetUserManager&lt;ApplicationUserManager&gt;();
  
          var roleManager = HttpContext.Current
              .GetOwinContext()
              .Get&lt;ApplicationRoleManager&gt;();
  
          const string name = "admin@admin.com";
          const string password = "Admin@123456";
          const string roleName = "Admin";
  
          //Create Role Admin if it does not exist
          var role = roleManager.FindByName(roleName);
  
          if (role == null) 
          {
              role = new IdentityRole(roleName);
              var roleresult = roleManager.Create(role);
          }
  
          var user = userManager.FindByName(name);
  
          if (user == null) 
          {
              user = new ApplicationUser { UserName = name, Email = name };
              var result = userManager.Create(user, password);
              result = userManager.SetLockoutEnabled(user.Id, false);
          }
  
          // Add user admin to Role Admin if not already added
          var rolesForUser = userManager.GetRoles(user.Id);
  
          if (!rolesForUser.Contains(role.Name)) 
          {
              var result = userManager.AddToRole(user.Id, role.Name);
          }
      }
  }
  </code></pre>
  
  <p>I moved InitializeIdentityForEF code into my configuration.cs file and added these two references and project builds successfully.</p>
  
  <pre><code>using Microsoft.AspNet.Identity.Owin;
  using Microsoft.AspNet.Identity;
  </code></pre>
  
  <p>But I get the following error when I run update-database from the Package Manager Console:</p>
  
  <p>System.NullReferenceException: Object reference not set to an instance of an object.
     at System.Web.HttpContextExtensions.GetOwinEnvironment(HttpContext context)
     at System.Web.HttpContextExtensions.GetOwinContext(HttpContext context)</p>
  
  <p><strong>Update</strong></p>
  
  <p>Per OdeToCode comment, you cannot use OWIN Package Manager Console: 
  Look at line 51 here: raw.githubusercontent.com/OdeToCode/MVC5_Samples/master/… I don't think you'll have success using Owin in the Seed method unless you are running Seed from inside the app. Owin won't be around or configured if you run Seed from the package manager console. –  OdeToCode May 13 at 15:00</p>
  
  <p>This <a href="https://stackoverflow.com/a/23861288/2330272">solution</a> has promise but the build has the following error:</p>
  
  <p>Error   4   The type 'Foo.Models.<strong>ApplicationUser</strong>' cannot be used as type parameter 'TUser' in the generic type or method 'Microsoft.AspNet.Identity.EntityFramework.UserStore'. There is no implicit reference conversion from 'Foo.Models.ApplicationUser' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'. c:\foo\Migrations\Configuration.cs  86  43  </p>
  
  <pre><code>protected override void Seed(Repository.DataContext.IdentityDb context)
      var roleStore = new RoleStore&lt;IdentityRole&gt;(context);
      var roleManager = new RoleManager&lt;IdentityRole&gt;(roleStore);
      var userStore = new UserStore&lt;ApplicationUser&gt;(context);
      var userManager = new UserManager&lt;ApplicationUser&gt;(userStore);               
      var user = new ApplicationUser { UserName = "sallen" };
  
      userManager.Create(user, "password");                    
      roleManager.Create(new IdentityRole { Name = "admin" });
      userManager.AddToRole(user.Id, "admin");
  }
  </code></pre>