title: No users have been created during Seed method using UserManager in ASP .NET MVC applications
body: <p>When the <code>Seed</code>method runs, records/objects are added to my database. </p>
  
  <p>Every object is added as it should till I try add users to my application(at the bottom of <code>Seed</code> method). No one is added. Also there is lot of <code>SQL</code> exceptions thrown posted at the bottom. They are thrown even if <code>Seed</code> method is empty.</p>
  
  <p>How to add users to Entity Framework managed database? I created my code following Scott Allen's tutorial.</p>
  
  <pre><code> protected override void Seed(WebApplication2.Models.ApplicationDbContext context) {
  
              System.Diagnostics.Debug.WriteLine("Seed started");//IT WORKS
  
              if (!context.Persons.Any()) {
                  var persons = new List&lt;Person&gt; { 
               new Person{FirstName = "John", LastName = "Doe", CellNumber = "123-456-789", SecondaryPhoneNumber = "98873213", Address = "1street 2",BirthDate = DateTime.Now.Date, Pesel = "312312312", Notes = "Annoying"},
               new Person{FirstName = "Anna", LastName = "Doe", CellNumber = "113-456-789", SecondaryPhoneNumber = "98873213", Address = "1street 2",BirthDate = DateTime.Now.Date, Pesel = "548555672", Notes = "Less Annoying"}
              };
  
                  persons.ForEach(person =&gt; context.Persons.AddOrUpdate(person));
                  context.SaveChanges();
              }
  
              if (!context.Meetings.Any()) {
                  var meetings = new List&lt;Meeting&gt;{
                  new Meeting{PersonId = 1, Body = "Body of meeting", Date = DateTime.Now}
              };
  
  
                  meetings.ForEach(meeting =&gt; context.Meetings.AddOrUpdate(meeting));
                  context.SaveChanges();
              }
              if (!context.Statuses.Any()) {
                  var statuses = new List&lt;Status&gt; {
                  new Status{Name = "OK"},
                  new Status {Name = "NOT_OK"}
              };
  
  
  
                  statuses.ForEach(status =&gt; context.Statuses.AddOrUpdate(status));
                  context.SaveChanges();
  
              }
             //EVERYTHING TILL NOW WORKS
              //Users Seeding
  
              if (!context.Users.Any()) {
                  System.Diagnostics.Debug.WriteLine("USER SEED");
                  try {
                      var store = new UserStore&lt;ApplicationUser&gt;(context);
                      var manager = new UserManager&lt;ApplicationUser&gt;(store);
                      //why user is not created
                      var user1 = new ApplicationUser { UserName = "admin", Email = "informatyka4444@wp.pl" };
                      var user2 = new ApplicationUser { UserName = "emp", Email = "informatyka4444@wp.pl" };
                      manager.Create(user1, "admin");
                      manager.Create(user2, "emp");
  
                      context.SaveChanges();
                  } catch (Exception e) { System.Diagnostics.Debug.WriteLine("THERE WAS AN EXCEPTION"); }
              }
  
  
          }
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I added some prints to the section where I add users also I posted output with some SQL exceptions.</p>
  
  <pre><code>if (!context.Users.Any()) {
              System.Diagnostics.Debug.WriteLine("USER SEED");
              try {
                  System.Diagnostics.Debug.WriteLine("1");
                  var store = new UserStore&lt;ApplicationUser&gt;(context);
                  var manager = new UserManager&lt;ApplicationUser&gt;(store);
                  //why user is not created
                  System.Diagnostics.Debug.WriteLine("2");
                  var user1 = new ApplicationUser { UserName = "admin", Email = "informatyka4444@wp.pl" };
                  var user2 = new ApplicationUser { UserName = "emp", Email = "informatyka4444@wp.pl" };
                  System.Diagnostics.Debug.WriteLine("3");
                  manager.Create(user1, "admin");
                  manager.Create(user2, "emp");
                  System.Diagnostics.Debug.WriteLine("4");
                  context.SaveChanges();
                  System.Diagnostics.Debug.WriteLine("5");
              } catch (Exception e) { System.Diagnostics.Debug.WriteLine("THERE WAS AN EXCEPTION"); }
              System.Diagnostics.Debug.WriteLine("6");
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>CONSTRCTOR
  CONSTRCTOR
  A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
  A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
  A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
  A first chance exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.dll
  A first chance exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.dll
  A first chance exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.dll
  A first chance exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll
  CONSTRCTOR
  Seed started
  USER SEED
  1
  2
  3
  'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/14/ROOT-1-130527942456023568): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\mscorlib.resources\v4.0_4.0.0.0_pl_b77a5c561934e089\mscorlib.resources.dll'. Module was built without symbols.
  4
  5
  6
  'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/14/ROOT-1-130527942456023568): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel.Internals\v4.0_4.0.0.0__31bf3856ad364e35\System.ServiceModel.Internals.dll'. Symbols loaded.
  </code></pre>
  
  <hr>
  
  <p>EDIT 2</p>
  
  <p>Here is <code>CONSTRUCTOR</code>:</p>
  
  <p><strong>Models.IdentityModels.cs</strong></p>
  
  <pre><code>using System.Security.Claims;
  using System.Threading.Tasks;
  using Microsoft.AspNet.Identity;
  using Microsoft.AspNet.Identity.EntityFramework;
  using System.Data.Entity;
  using System.Data.Entity.ModelConfiguration.Conventions;
  
  namespace WebApplication2.Models {
      // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
      public class ApplicationUser : IdentityUser {
          public async Task&lt;ClaimsIdentity&gt; GenerateUserIdentityAsync(UserManager&lt;ApplicationUser&gt; manager) {
              // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
              var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
              // Add custom user claims here
              return userIdentity;
          }
      }
  
      public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt; {
          public ApplicationDbContext()
              : base("DefaultConnection", throwIfV1Schema: false) {
              System.Diagnostics.Debug.WriteLine("CONSTRCTOR");
  
          }
  
  
  
          public DbSet&lt;Person&gt; Persons { get; set; }
          public DbSet&lt;Meeting&gt; Meetings { get; set; }
          public DbSet&lt;Status&gt; Statuses { get; set; }
          protected override void OnModelCreating(DbModelBuilder modelBuilder) {
              base.OnModelCreating(modelBuilder);
              modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
          }
  
          public static ApplicationDbContext Create() {
              return new ApplicationDbContext();
          }
      }
  }
  </code></pre>
  
  <p><strong>Migrations.Confriguration.cs</strong></p>
  
  <pre><code>namespace WebApplication2.Migrations {
      using Microsoft.AspNet.Identity;
      using Microsoft.AspNet.Identity.EntityFramework;
      using System;
      using System.Collections.Generic;
      using System.Data.Entity;
      using System.Data.Entity.Migrations;
      using System.Linq;
      using WebApplication2.Models;
  
      internal sealed class Configuration : DbMigrationsConfiguration&lt;WebApplication2.Models.ApplicationDbContext&gt; {
          public Configuration() {
              AutomaticMigrationsEnabled = true;
              ContextKey = "WebApplication2.Models.ApplicationDbContext";
          }
  
          protected override void Seed(WebApplication2.Models.ApplicationDbContext context) {
              /*
                          System.Diagnostics.Debug.WriteLine("Seed started");
                          if (!context.Persons.Any()) {
                              var persons = new List&lt;Person&gt; { 
                           new Person{FirstName = "John", LastName = "Doe", CellNumber = "123-456-789", SecondaryPhoneNumber = "98873213", Address = "1street 2",BirthDate = DateTime.Now.Date, Pesel = "312312312", Notes = "Annoying"},
                           new Person{FirstName = "Anna", LastName = "Doe", CellNumber = "113-456-789", SecondaryPhoneNumber = "98873213", Address = "1street 2",BirthDate = DateTime.Now.Date, Pesel = "548555672", Notes = "Less Annoying"}
                          };
  
                              persons.ForEach(person =&gt; context.Persons.AddOrUpdate(person));
                              context.SaveChanges();
                          }
  
                          if (!context.Meetings.Any()) {
                              var meetings = new List&lt;Meeting&gt;{
                              new Meeting{PersonId = 1, Body = "Body of meeting", Date = DateTime.Now}
                          };
  
  
                              meetings.ForEach(meeting =&gt; context.Meetings.AddOrUpdate(meeting));
                              context.SaveChanges();
                          }
                          if (!context.Statuses.Any()) {
                              var statuses = new List&lt;Status&gt; {
                              new Status{Name = "OK"},
                              new Status {Name = "NOT_OK"}
                          };
  
  
  
                              statuses.ForEach(status =&gt; context.Statuses.AddOrUpdate(status));
                              context.SaveChanges();
  
                          }*/
              //Users Seeding
  
              if (!context.Users.Any()) {
                  System.Diagnostics.Debug.WriteLine("USER SEED");
                  try {
                      System.Diagnostics.Debug.WriteLine("1");
                      var store = new UserStore&lt;ApplicationUser&gt;(context);
                      var manager = new UserManager&lt;ApplicationUser&gt;(store);
                      //why user is not created
                      System.Diagnostics.Debug.WriteLine("2");
                      var user1 = new ApplicationUser { UserName = "admin", Email = "informatyka4444@wp.pl" };
                      var user2 = new ApplicationUser { UserName = "emp", Email = "informatyka4444@wp.pl" };
                      System.Diagnostics.Debug.WriteLine("3");
                      manager.Create(user1, "admin");
                      manager.Create(user2, "emp");
                      System.Diagnostics.Debug.WriteLine("4");
                      context.SaveChanges();
                      System.Diagnostics.Debug.WriteLine("5");
                  } catch (Exception e) { System.Diagnostics.Debug.WriteLine("THERE WAS AN EXCEPTION"); }
                  System.Diagnostics.Debug.WriteLine("6");
              }
  
  
          }
      }
  }
  </code></pre>
  
  <p><strong>Global.asax</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data.Entity;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using System.Web.Optimization;
  using System.Web.Routing;
  using WebApplication2.Migrations;
  using WebApplication2.Models;
  
  
  namespace WebApplication2 {
      public class MvcApplication : System.Web.HttpApplication {
          protected void Application_Start() {
              System.Diagnostics.Debug.WriteLine("Application_Start");
  
             Database.SetInitializer(new MigrateDatabaseToLatestVersion&lt;ApplicationDbContext, Configuration&gt;());
             new ApplicationDbContext().Database.Initialize(true);
              AreaRegistration.RegisterAllAreas();
              FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
              RouteConfig.RegisterRoutes(RouteTable.Routes);
              BundleConfig.RegisterBundles(BundleTable.Bundles);
          }
      }
  }
  </code></pre>
  
  <p><strong>Web.config</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!--
    For more information on how to configure your ASP.NET application, please visit
    http://go.microsoft.com/fwlink/?LinkId=301880
    --&gt;
  &lt;configuration&gt;
    &lt;configSections&gt;
  
      &lt;section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /&gt;
    &lt;!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --&gt;&lt;/configSections&gt;
    &lt;connectionStrings&gt;
      &lt;add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication2-20140711041006.mdf;Initial Catalog=aspnet-WebApplication2-20140711041006;Integrated Security=True" providerName="System.Data.SqlClient" /&gt;
    &lt;/connectionStrings&gt;
    &lt;appSettings&gt;
      &lt;add key="webpages:Version" value="3.0.0.0" /&gt;
      &lt;add key="webpages:Enabled" value="false" /&gt;
      &lt;add key="ClientValidationEnabled" value="true" /&gt;
      &lt;add key="UnobtrusiveJavaScriptEnabled" value="true" /&gt;
    &lt;/appSettings&gt;
    &lt;system.web&gt;
      &lt;authentication mode="None" /&gt;
      &lt;compilation debug="true" targetFramework="4.5.1" /&gt;
      &lt;httpRuntime targetFramework="4.5.1" /&gt;
    &lt;/system.web&gt;
    &lt;system.webServer&gt;
      &lt;modules&gt;
        &lt;remove name="FormsAuthenticationModule" /&gt;
      &lt;/modules&gt;
    &lt;/system.webServer&gt;
    &lt;runtime&gt;
      &lt;assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /&gt;
          &lt;bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /&gt;
          &lt;bindingRedirect oldVersion="1.0.0.0-5.1.0.0" newVersion="5.1.0.0" /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" /&gt;
          &lt;bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /&gt;
          &lt;bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /&gt;
        &lt;/dependentAssembly&gt;
        &lt;dependentAssembly&gt;
          &lt;assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /&gt;
          &lt;bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /&gt;
        &lt;/dependentAssembly&gt;
      &lt;/assemblyBinding&gt;
    &lt;/runtime&gt;
    &lt;entityFramework&gt;
     &lt;!-- &lt;contexts&gt;
       &lt;context type="WebApplication2.Models.ApplicationDbContext, WebApplication2"&gt;
          &lt;databaseInitializer type="WebApplication2.Migrations.Configuration, WebApplication2" /&gt;
        &lt;/context&gt;
      &lt;/contexts&gt;--&gt;
      &lt;defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /&gt;
      &lt;providers&gt;
        &lt;provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /&gt;
      &lt;/providers&gt;
    &lt;/entityFramework&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT 3(partialy solved problem)</strong>: This below did the job. But why the way introduced in MVC 5(User Manager above) does not work?</p>
  
  <pre><code>System.Diagnostics.Debug.WriteLine("BEFORE " + !context.Users.Any());
  
              if (!context.Users.Any()) {
                  System.Diagnostics.Debug.WriteLine("INSIDE");
                  var hasher = new PasswordHasher();
                  var users = new List&lt;ApplicationUser&gt; { 
                          new ApplicationUser{UserName = "admin", PasswordHash = hasher.HashPassword("admin")}
                          };
  
                  users.ForEach(user =&gt; context.Users.AddOrUpdate(user));
                  context.SaveChanges();
              }
  </code></pre>
  
  <hr>
  
  <p>Current version of Migrations/<em>(directory)</em>/.Configuration.cs with <code>Seed</code> method inside:</p>
  
  <pre><code>namespace WebApplication2.Migrations {
      using Microsoft.AspNet.Identity;
      using System;
      using System.Collections.Generic;
      using System.Data.Entity.Migrations;
      using System.Data.Entity.Validation;
      using System.Linq;
      using WebApplication2.Models;
  
      internal sealed class Configuration : DbMigrationsConfiguration&lt;WebApplication2.Models.ApplicationDbContext&gt; {
          public Configuration() {
              AutomaticMigrationsEnabled = true;
              ContextKey = "WebApplication2.Models.ApplicationDbContext";
          }
  
          protected override void Seed(WebApplication2.Models.ApplicationDbContext context) {
  
              System.Diagnostics.Debug.WriteLine("Seed started");
              if (!context.Persons.Any()) {
                  var persons = new List&lt;Person&gt; { 
                           new Person{FirstName = "John", LastName = "Doe", CellNumber = "123-456-789", SecondaryPhoneNumber = "98873213", Address = "1street 2",BirthDate = DateTime.Now.Date, Pesel = "312312312", Notes = "Annoying"},
                           new Person{FirstName = "Anna", LastName = "Doe", CellNumber = "113-456-789", SecondaryPhoneNumber = "98873213", Address = "1street 2",BirthDate = DateTime.Now.Date, Pesel = "548555672", Notes = "Less Annoying"}
                          };
  
                  persons.ForEach(person =&gt; context.Persons.AddOrUpdate(person));
                  context.SaveChanges();
              }
  
              if (!context.Meetings.Any()) {
                  var meetings = new List&lt;Meeting&gt;{
                              new Meeting{PersonId = 1, Body = "Body of meeting", Date = DateTime.Now}
                          };
  
  
                  meetings.ForEach(meeting =&gt; context.Meetings.AddOrUpdate(meeting));
                  context.SaveChanges();
              }
              if (!context.Statuses.Any()) {
                  var statuses = new List&lt;Status&gt; {
                              new Status{Name = "OK"},
                              new Status {Name = "NOT_OK"}
                          };
  
  
  
                  statuses.ForEach(status =&gt; context.Statuses.AddOrUpdate(status));
                  context.SaveChanges();
  
              }
              //Users Seeding
              System.Diagnostics.Debug.WriteLine("BEFORE " + !context.Users.Any());
  
  
  
              if (!context.Users.Any()) {
                  System.Diagnostics.Debug.WriteLine("INSIDE");
                  var hasher = new PasswordHasher();
                  try {
                      var users = new List&lt;ApplicationUser&gt; { 
                          new ApplicationUser{PasswordHash = hasher.HashPassword("TestPass44!"), Email = "informatyka4444@wp.pl", UserName = "informatyka4444@wp.pl"},
                          new ApplicationUser{PasswordHash = hasher.HashPassword("TestPass44!"), Email = "informatyka4445@wp.pl", UserName = "informatyka4445@wp.pl"}
                          };
  
                      users.ForEach(user =&gt; context.Users.AddOrUpdate(user));
  
                      context.SaveChanges();
                  } catch (DbEntityValidationException e) {
                      System.Diagnostics.Debug.WriteLine("EXC: ");
                      foreach (DbEntityValidationResult result in e.EntityValidationErrors) {
                          foreach (DbValidationError error in result.ValidationErrors) {
                              System.Diagnostics.Debug.WriteLine(error.ErrorMessage);
                          }
                      }
  
                  }
              }
  
  
          }
      }
  }
  </code></pre>