Borek Bernard
Borek Bernard

Reputation: 53352

Entity Framework didn't apply migrations using Database.SetInitializer()

I have a simple ASP.NET Web Pages site using Entity Framework 6.1 (the Web Pages live in the standard project with csproj so that I could use EF Code First with migrations). This is in my _AppStart.cshtml:

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

It all compiles fine, I have confirmed that the code indeed go through this line but the migrations aren't applied - I have confirmed that by manually executing Update-Database which did the work correctly.

What could be wrong? Do I need to set something else for it to work?

Upvotes: 0

Views: 110

Answers (1)

mr100
mr100

Reputation: 4428

Calling SetInitializer (as name says) does not create database or apply migrations. It just sets the initializer. It will be invoked for instance when you would like to save data to db with SaveChanges method and the database won't exist at that time. To force immediate migration you should use DbMigrator class:

var migrator = new DbMigrator(new MyMigrationsConfiguration());
migrator.Update();

Upvotes: 1

Related Questions