My Seed() method is never called in Code First EF 5

code-first ef-code-first entity-framework


My Seed() method never gets used. When I perform an it is known asUpdate-Database when I run from code, but never when I run through the Package Manager Console. My migration classes are executed and all tables are created if I delete my database, but my Seed() method is never called. Code First, MVC 4, Entity Frame Work 5.


protected void Application_Start()
  Database.SetInitializer<MyContext>(new DbInitializer());


internal class DbInitializer : MigrateDatabaseToLatestVersion<MyContext, Migrations.Configuration>


public partial class MyContext : DbContext
  public MyContext() : base("DefaultConnection")
  // public DBSets....


internal sealed class Configuration : DbMigrationsConfiguration<MyContext>
public Configuration()
  // The constructor is actually called
  AutomaticMigrationsEnabled = false;

protected override void Seed(MyContext context)
   // My seed code, never called

What might be a problem?

11/9/2012 7:41:34 PM

Accepted Answer

As a result, I had to define my unique Initializer in the configuration file.

        <context type="EFTest2.MyContext, EFTest2">
          <databaseInitializer type="EFTest2.Initializers.DbInitializer, EFTest2" />
      <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />

Afterwards, mySeed a method is accessed.

11/5/2013 8:38:55 AM

Popular Answer

Please start by looking at the correct response.
I just want to make a very significant addition to this topic.

I experienced the identical issue as indicated in this inquiry, which was EXACTLY (and this lead me to here). However, even after applying the correct answer, my seed method was not used because I was using CreateDatabaseIfNotExists rather than MigrateDatabaseToLatestVersion.

My issue was as follows: The evidence of the Seed method's documentation states that:Seed approach to theDbMigrationsConfiguration will not be carried out if one of the following describes the database initializer.

In order to override the seed function in your own class, which inherits from one of those kinds if you are using one of those types, you must be using one of those types.

In my situation, adding the subsequent class fixed the issue.

public class CreateNotifierDatabaseIfNotExists : CreateDatabaseIfNotExists<NotifierContext>
    protected override void Seed(NotifierContext context)
        // the code of the seeding is go here

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow