Enable AutoMigrations as well as other migrations in Entity Framework.

asp.net-mvc-3 c# ef-migrations entity-framework


I'm utilizing Entity Framework 4.3 Migrations in my project. I would like to use Automatic migrations so that when I make modifications to my domain objects and my context class, my database automatically updates when I run the project. I have this working so far.

I would also like to use some Added Migrations in addition to the automatic migrations, and I would like the application to automatically jump to the latest version (based on my added migrations) when I run the application.

In order to do this I have placed this in the global.asax file...

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Core.Migrations.Configuration>());

Now this works, but when I do this it no longer automatically updates the database based on my domain objects.

I would like to be able to completely delete the database and then run the application and have all the automatic migrations run and then have my explicit migrations run and bring the database up to the latest version.

I know I've had this working in a previous project, but I'm not sure what I'm doing wrong in this instance.


5/18/2012 9:07:47 AM

Accepted Answer

You need to pass a configuration that has the AutomaticMigrationsEnabled set to true in the constructor. Something like this should help:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, MyConfiguration>());

with MyConfiguration being something like:

public class MyConfiguration : Core.Migrations.Configuration
    public MyConfiguration { this.AutomaticMigrationsEnabled = true; }

DISCLAIMER: Just hacked this in, so small tweaks might be required to get this to compile


Just checked with EF 4.3.1 and the code is like this for the initializer:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<DataContext, MyConfiguration>());

and this for the configuration class:

public class MyConfiguration : System.Data.Entity.Migrations.DbMigrationsConfiguration<DataContext>
    public MyConfiguration()
        this.AutomaticMigrationsEnabled = true;
5/18/2012 11:42:57 AM

Popular Answer

After banging my head on this for several hours, I finally came up with a solution that creates the database if necessary or upgrades it if out of date. We use this technique in Gallery Server Pro to make it easy to install the first time or upgrade previous versions.

private static void InitializeDataStore()
  System.Data.Entity.Database.SetInitializer(new System.Data.Entity.MigrateDatabaseToLatestVersion<GalleryDb, GalleryDbMigrationConfiguration>());

  var configuration = new GalleryDbMigrationConfiguration();
  var migrator = new System.Data.Entity.Migrations.DbMigrator(configuration);
  if (migrator.GetPendingMigrations().Any())

public sealed class GalleryDbMigrationConfiguration : DbMigrationsConfiguration<GalleryDb>
  protected override void Seed(GalleryDb ctx)

I wrote up a blog post with a few more details: Using Entity Framework Code First Migrations to auto-create and auto-update an application

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