Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Migrations - Enable AutoMigrations along with added migration

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.

Thanks

like image 238
jdavis Avatar asked May 18 '12 02:05

jdavis


1 Answers

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

EDIT:

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;
    }
}
like image 125
Mirko Avatar answered Sep 28 '22 06:09

Mirko