MigrateDatabaseToLatestVersion not executed

I can't figure out why my latest migration is not getting executed automatically on application startup (or at least on first access of the database context). I used to run update-database manually in development, but I want to test whether it will upgrade automatically on my hosted test environment.

In Application_Start():

    new MigrateDatabaseToLatestVersion<FepazoContext, FepazoConfiguration>())

In FepazoConfiguration:

internal sealed class FepazoConfiguration : 
    public FepazoConfiguration()
        AutomaticMigrationsEnabled = true;

I even added this to the constructor of FepazoContext:

public FepazoContext() : base("DefaultConnection")

Some extra information:

  • The migration was automatically created through add-migration and looks ok.
  • When I query the __MigrationHistory table, I can see that the migration is not yet 'recorded' as executed.
  • I verified that the initializer or the AutomaticMigrationsEnabled setting isn't overridden in Web.config file.
  • Breakpoints in FepazoContext constructor and the FepazoConfiguration are getting hit.

Am I forgetting something ? Can I dig deeper to find out where it goes wrong ?


Passing True to Database.Initialize to try and force the migration also has no effect. Database.CompatibleWithModel(true) returns false - so the system detects there is a difference, however it does not execute the pending migration!

public FepazoContext() : base("DefaultConnection")
    if (!Database.CompatibleWithModel(true))
        // This is executed (each time the code enters)


As a workaround, I call DbMigrator.Update() explicitly right after setting the initializer. That does the trick, although I'd still like to know why it doesn't work automatically...

protected void Application_Start()
    // <...>
        new MigrateDatabaseToLatestVersion<FepazoContext, FepazoConfiguration>());
    var dbMigrator = new DbMigrator(new FepazoConfiguration());
    // <...>

According to another answer on here the initializer doesn't run until there is some interaction with the database. The answer explains how to force the initializer to run immediately.

MigrateDatabaseToLatestVersion not executed, According to another answer on here the initializer doesn't run until there is some interaction with the database. The answer explains how to  What I am looking for I want MigrateDatabaseToLatestVersion to automatically detect if there is an existing database. If there is not, it will create it for me. If there is an existing database, it will match the entities to the tables and detect if there has been a change. If there has, it will automatically update the database for me.

In the above code snippet you are calling the Database.Initialize() method immediately after creating a context instance. In this case, the database will be created immediately after calling the Initialize() method instead of waiting until the context is used for the first time.

The Initialize() method takes a Boolean parameter that controls whether the initialization process should re-run if it has already run for the application.

Specifying false will skip the initialization process if it has already executed. A value of true will initialize the database again even if it was already initialized.

update-database works, but MigrateDatabaseToLatestVersion , I have run the debugger and it hits the configuration file, and it hits the have different connection string, my context file is not in mvc project,  Tutorial: Use EF Migrations in an ASP.NET MVC app and deploy to Azure. 01/16/2019; 20 minutes to read +5; In this article. So far the Contoso University sample web application has been running locally in IIS Express on your development computer.

I had this issue. My problem turned out to be that I had a MigrationsContextFactory that was being used to provide the connection string. When the database initialization was occurring the migration context factory was being called. This MigrationsContextFactory was getting a connection string to a different database and was ensuring that was up to date.

I removed the MigrationsContextFactory and passed true to MigrateDatabaseToLatestVersion<,> Migrator to tell it to use the current context. See this question top voted answer How do I inject a connection string into an instance of IDbContextFactory<T>?

Data Points, This is to ensure that when you execute migrations, Code First doesn't attempt to create an There's no MigrateDatabaseToLatestVersion initializer in play and  Unless the Migrations initializer is being used the Migrations Seed method will not be executed when your application starts. It must handle cases where the database already contains data because Migrations is evolving the database rather than dropping and recreating it.

Make sure the MigrateDatabaseToLatestVersion will be using the correct context that the application is using. This is done by using the parameter useSuppliedContext.

    new MigrateDatabaseToLatestVersion<FepazoContext, FepazoConfiguration>(useSuppliedContext: true));

After this change, my MVC application did update my database.

Clarify how this configuration play along when using , I have run into an issue where my app crash because the MigrateDatabaseToLatestVersion seem to not be aware of the new name of the  The code-based migration provides more control on the migration and allows you to configure additional things such as setting a default value of a column, configure a computed column etc. In order to use code-based migration, you need to execute the following commands in the Package Manager Console in Visual Studio:

Automated Migration in Entity Framework 6, The automated migrations can be implemented by executing the enable-​migrations in the context class to MigrateDatabaseToLatestVersion , as shown below. Last week I came across a tough programming challenge that I couldn’t find solved anywhere on the internet. It took several hours to figure it out, so I thought I’d write it up here to help others. Gallery Server Pro uses Entity Framework (EF) Code First Migrations to create the database and tables and seed it

Using Entity Framework Code First Migrations to auto-create and , When a migration was added and applied to an instance running an older If the MigrateDatabaseToLatestVersion initializer was working  What I’m not getting with this pattern that I liked about the DropCreateDatabaseAlways initializer is a fresh version of the database for integration tests. To achieve this, I have to execute some raw SQL in the Seed method of the configuration file prior to seeding the database.

Quick Tip: Run EntityFramework Migration Seeds for , If you're using the MigrateDatabaseToLatestVersion initializer option (rather common) the seed method won't be called database then run the Seed() method to populate the data if necessary. No second calls needed! To deploy a Code First database to an Azure Web Site, you can use the Execute Code First Migrations check box in the Publish Web wizard: When you select that check box, Visual Studio configures the destination web site so that Entity Framework automatically deploys the database or updates it by running the MigrateDatabaseToLatestVersion initializer on application start,

  • Of course, that's why I added "(or at least on first access of the database context)" in my first sentence, and why I said I tried Database.Initialize(true); without effect. Unfortunately the solution to this problem isn't that straightforward ;)
  • Wow, I spend 15 min looking into this issue, as I was starting my app but not logging in (/interacting with application), indeed it must interact with DB to perform the migration : ' )
  • Thanks for your answer. That doesn't explain why the system doesn't detect that it has to update the database using the latest migration though. I tried passing true as parameter to Database.Initialize() to force the upgrade, but even that doesn't execute it.
  • I assumed that since IDatabaseInitializer<TContext>.InitializeDatabase(TContext) accepted a TContext parameter it always just would use the supplied context. All of the other built-in initializers don’t even have that option, so it’s weird that they gave it inconsistent behavior with the rest of the framework. (Sorry for rant).