简体   繁体   中英

Seed method in EF Migrations configuration.cs not being called

I'm facing a weird issue with EF Migrations. I have it enabled for an MVC website and the Migrations folder has 2 assets - - _InitialMigration(this is the base code first model of my project) - Configuration.cs (I need the seed method here to fire on new DB creation)

This works as expected on the machine I enabled EF Migrations on. However after checking the code in to TFS. On any other machine, the Seed method never fires. I have ensured that there is no trace of the DB on those machines. The DB schema gets created when the project runs and the constructor in the Configuation.cs gets fired. However the Seed() method never fires. The only way I can get this to work on any other machine is to manually run 'Add-Migration Initial' from package manager console on each of those machines which results in updating the existing _Initial.cs migration file and checking it out. After this the seed method runs successfully if the DB is just created.

in my Global.asax I have the following code

Database.SetInitializer(new MigrateDatabaseToLatestVersion<Context, Configuration>());
using (var context = new Context())
{
    context.Database.Initialize(true);
}

The Configuration.cs is as follows

public sealed class Configuration : DbMigrationsConfiguration<Context>
{
    private bool pendingMigrationsExist;
    private Seeder seeder;

    public Configuration()
    {
        // This ensures a schema diff. is always performed and automatic updates take place.
        AutomaticMigrationsEnabled = true;

        // This is required to detect changes.
        pendingMigrationsExist = new DbMigrator(this).GetPendingMigrations().Any();            
    }

    protected override void Seed(Context context)
    {            
        // Only if a diff exists should migrations be performed.
        if(!pendingMigrationsExist)
        {
            return;
        }

        // Initialize Seeding routine and populate the target DB.
        seeder = new Seeder(context);
        seeder.Seed();
    }        
}

Figured it out. I needed this little snippet

public Configuration()
    {
        // This ensures a schema diff. is always performed and automatic updates take place.
        AutomaticMigrationsEnabled = true;

        var dbMigrator = new DbMigrator(this);

        // This is required to detect changes.
        pendingMigrationsExist = dbMigrator.GetPendingMigrations().Any();

        if (pendingMigrationsExist)
        {
            dbMigrator.Update();
        }

    }

The explicit call to update did it. This article goes in depth around such scenarios: http://blog.oneunicorn.com/2013/05/28/database-initializer-and-migrations-seed-methods/

I'm not sure what the purpose of your pendingMigrationsExist flag is. The Seed method is called when any migrations are run - if no migrations are run it will not be called.

Check that this flag is not what is preventing your seed method from running.

Additionally, I would recommend that you use explicit migrations over automatic migrations, so that you have control over what migrations run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM