Neil
Neil

Reputation: 5239

Entity Framework 5 Automatic Code Migrations

In my current web project I'm trying to set up code migrations. I set up my db initialiser as follows in my MVC4 project

protected void Application_Start()
{
    Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
}

I can make changes to my code first models and this updates the database to the latest automatic migration as expected when I start up the website.

However I'm trying to add an explicit migration through the console Add-Migration FirstMigration so I can add some indexes. This adds the code file 201301071708126_FirstMigration to my project and I can add in my index code here easily enough.

But this won't run automatically when I restart the website. I have to run Update-Database from the console to apply this migration.

I followed this tutorial on msdn but I can't see what I'm doing wrong as it suggests to me that the explicit migration should run automatically.

In my configuration file I have the following constructor

public Configuration()
{
    AutomaticMigrationsEnabled = true;
    AutomaticMigrationDataLossAllowed = true;
}

Upvotes: 4

Views: 4313

Answers (2)

Neil
Neil

Reputation: 5239

I found the answer to this a little while ago and I missed out some information in the question.

I have 2 websites, an MVC app and a WEBAPI app. I need to have the database initialisation in both otherwise it doesn't work properly.

Upvotes: 4

Pedro.The.Kid
Pedro.The.Kid

Reputation: 2078

in my application constructor I have

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MYContext>());

and it updates the model.

Upvotes: -1

Related Questions