Вопрос

I have EF migrations enabled and want to update an existing database running in azure. When I run the application I get the following exception:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: The model backing the 'MyContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

However I have migrations enabled.

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

And

internal sealed class Configuration : DbMigrationsConfiguration<MyContext>
{
    public Configuration()
    {
         AutomaticMigrationsEnabled = false;//have also tried true
    }
}

Up until this latest commit everything has worked automatically as expected and i haven't changed the initialization code (only added new migrations).

If i download the database and run the app I get the same result. However if I manually run Update-Database everything works as expected.

Is there a way to get this to run via code as I cant run Update-Database inside Azure.

Это было полезно?

Решение

I found my (stupid) mistake, I hadn't changed the initialisation code but i had managed to deregister it so it wasn't getting called.

I moved it to the static constructor on my context and all was happy.

static MyContext()
{
    Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top