title: Identity Framework seed method does not save newly created user when called from Global.asax.cs
body: <p>I am trying to seed an "admin" account in Identity framework during application start up. The majority of our application is not set up through code-first Entity framework models, so I need to do this without extending one of the IDatabaseInitializer classes. I am using the same database as these database-first models.</p>
  
  <p>This is an ASP.NET MVC 5 application.</p>
  
  <p>In <code>Global.asax.cs</code>, I have the following relevant code.</p>
  
  <pre><code>using (var context = new IdentityContext(EnvironmentSettings.Current.DatabaseConnections.CreateDbConnection("Extranet").ConnectionString))
  {
      context.SeedAdminAccount(EnvironmentSettings.DefaultAdminAccount.UserName, EnvironmentSettings.DefaultAdminAccount.Password).Wait();
  }
  </code></pre>
  
  <p>The connection string is an Azure SQL server. The username is an email address, and the password is a string of characters, including a bang.</p>
  
  <p>The class IdentityContext looks like this.</p>
  
  <pre><code>public class IdentityContext : IdentityDbContext&lt;IdentityUser&gt;
  {
      public IdentityContext(string connectionString) : base(connectionString)
      {
          Debug.WriteLine(connectionString);
          Initialize();
      }
  
      void Initialize()
      {
          Database.Log = s =&gt; System.Diagnostics.Debug.WriteLine(s);
          Database.SetInitializer&lt;IdentityContext&gt;(new CreateInitializer());
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
  
          modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
  
          modelBuilder.Entity&lt;IdentityUser&gt;().ToTable("IdentityUser", "dbo");
          modelBuilder.Entity&lt;IdentityRole&gt;().ToTable("IdentityRole", "dbo");
          modelBuilder.Entity&lt;IdentityUserClaim&gt;().ToTable("IdentityUserClaim", "dbo");
          modelBuilder.Entity&lt;IdentityUserLogin&gt;().ToTable("IdentityUserLogin", "dbo");
          modelBuilder.Entity&lt;IdentityUserRole&gt;().ToTable("IdentityUserRole", "dbo");
      }
  }
  </code></pre>
  
  <p><code>context.SeedAdminAccount()</code> is an extension of <code>IdentityContext</code>. It looks like this.</p>
  
  <pre><code>public static class IdentityContextExtensions
  {
      public static async Task SeedAdminAccount(this IdentityContext identityContext, string username, string password)
      {
          var userManager = new UserManager&lt;IdentityUser&gt;(new UserStore&lt;IdentityUser&gt;(identityContext));
  
          //var user = await userManager.FindAsync(EnvironmentSettings.DefaultAdminAccount.UserName, EnvironmentSettings.DefaultAdminAccount.Password);
          var user = await userManager.FindAsync(username, password);
  
          if (user != null) return;
  
          user = new IdentityUser() { UserName = username };
  
          var role = new IdentityUserRole { Role = new IdentityRole(Role.Admin) };
          user.Roles.Add(role);
  
          await userManager.CreateAsync(user, password);
  
          identityContext.SaveChanges();
      }
  }
  </code></pre>
  
  <p>And lastly, <code>CreateInitializer</code> looks like this (although it is not called).</p>
  
  <pre><code>public class CreateInitializer : CreateDatabaseIfNotExists&lt;IdentityContext&gt;
  {
      protected override async void Seed(IdentityContext context)
      {
          var user = new
          {
              EnvironmentSettings.DefaultAdminAccount.UserName,
              EnvironmentSettings.DefaultAdminAccount.Password
          };
  
          await context.SeedAdminAccount(user.UserName, user.Password);
  
          base.Seed(context);
      }
  }
  </code></pre>
  
  <p>Okay, so with all of that out of the way, here's what works:</p>
  
  <p>1) Application startup successfully creates an instance of <code>IdentityContext</code>.</p>
  
  <p>2) Identity framework creates the correct tables with the modified table names.</p>
  
  <p>3) <code>SeedAdminAccount()</code> is called with the correct parameters.</p>
  
  <p>4) <code>userManager.FindAsync()</code> does not find the user (because it doesn't exist).</p>
  
  <p>5) <code>SeedAdminAccount()</code> continues to each statement in its body and returns successfully.</p>
  
  <p>Here's where I'm stuck:</p>
  
  <p>1) Although it appears that my seed method is working correctly, no rows are saved to the <code>IdentityUser</code> table, or any other Identity framework tables.</p>
  
  <p>2) If I use the same code from a controller action, a user is created and stored in the <code>IdentityUser</code> table successfully.</p>
  
  <p>What am I missing here? Am I using the wrong context during application start up? Is there some sort of exception happening that I can't see?</p>