title: How can I get my database to seed using Entity Framework CodeFirst?
body: <p>The database is created successfully (as are the tables) but is not seeded. I have spent several hours and read tons of articles but have not been able to get it. Any suggestions?</p>
  
  <p>On a side note, is it possible to call the initializer without having a reference to my DatabaseContext in the client?</p>
  
  <p>I have included all the relevant code I could think of. If anything else would be helpful, please let me know.</p>
  
  <p>Things I've Tried:</p>
  
  <ol>
  <li>I deleted my connection string (since it defaults to sqlexpress anyways, just the name changed)</li>
  <li>I changed DropCreateDatabaseIfModelChanges to DropCreateDatabaseAlways, still the same.</li>
  </ol>
  
  <p>Edit: The really weird thing is it worked once, but I have no idea how or why it broke again. I am assuming connection strings, but who knows.</p>
  
  <p>DatabaseInitializer.cs</p>
  
  <pre><code>public class DatabaseInitializer : DropCreateDatabaseIfModelChanges&lt;DatabaseContext&gt;
  {
    protected override void Seed(DatabaseContext context)
    {
      // Seeding data here
      context.SaveChanges();
    }
  }
  </code></pre>
  
  <p>DatabaseContext.cs</p>
  
  <pre><code>public class DatabaseContext : DbContext
  {
    protected override void OnModelCreating(DbModelBuilder mb)
    {
      // Random mapping code
    }
  
    public DbSet&lt;Entity1&gt; Entities1 { get; set; }
    public DbSet&lt;Entity2&gt; Entities2 { get; set; }
  
  }
  </code></pre>
  
  <p>Global.asax.cs - Application_Start()</p>
  
  <pre><code>protected void Application_Start()
  {
    Database.SetInitializer&lt;DatabaseContext&gt;(new DatabaseInitializer());
    AreaRegistration.RegisterAllAreas();
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
  }
  </code></pre>
  
  <p>Client web.config</p>
  
  <pre><code>&lt;connectionStrings&gt;
    &lt;add name="DatabaseContext" connectionString="data source=.\SQLEXPRESS;Database=Database;Integrated Security=SSPI;" providerName="System.Data.SqlClient" /&gt;
  &lt;/connectionStrings&gt;
  </code></pre>
  
  <h1>SOLUTION</h1>
  
  <p>For the sake of documentation, I am sharing my solution here. Navigating all the comments would be a pain anyways. In the end I had DatabaseInitializer and DatabaseContext in separate classes. I don't really understand while these tiny changes fixed it, but here it is.</p>
  
  <p>DatabaseInitializer.cs</p>
  
  <pre><code>public class DatabaseInitializer : CreateDatabaseIfNotExists&lt;DatabaseContext&gt;
  {
    protected override void Seed(DatabaseContext context)
    {
      // Seed code here
    }
  }
  </code></pre>
  
  <p>DatabaseContext.cs</p>
  
  <pre><code>public class DatabaseContext : DbContext
  {
    public DatabaseContext() : base("MyDatabase") { }
  
    protected override void OnModelCreating(DbModelBuilder mb)
    {
      // Code here
    }
  
    public DbSet&lt;Entity&gt; Entities { get; set; }
    // Other DbSets
  }
  </code></pre>
  
  <p>Global.asax.cs - Application_Start()</p>
  
  <pre><code>protected void Application_Start()
  {
    Database.SetInitializer(new DatabaseInitializer());
    AreaRegistration.RegisterAllAreas();
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
  }
  </code></pre>