Entity-framework – Automatically Create Database in Entity Framework 6 with Automatic Migrations Disabled

entity-frameworkentity-framework-6

I would like to be able to automatically create a new database, if it does not exist, using code-first EF6 but with automatic migrations disabled.

If i recall correctly, before automatic migrations existed in Entity Framework, this worked just fine. However, in EF6, I receive the following exception:

An exception of type 'System.Data.Entity.Migrations.Infrastructure.AutomaticMigrationsDisabledException'
occurred in EntityFramework.dll but was not handled in user code

Additional information: Unable to update database to match the current model because there are pending changes and automatic
migration is disabled. Either write the pending model changes to a
code-based migration or enable automatic migration. Set
DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable
automatic migration.

To verify that this exception was not caused by a external factor in my production project, I created a new test project. However, I get the same exception.

For my database context, I have:

public class MyContext : DbContext
{
    public DbSet<Entity> Entities { get; set; }

    public MyContext()
    {
        Database.SetInitializer(new CreateDatabaseIfNotExists<MyContext>());
    }
}

Then I added a database migrations configuration file to disable automatic migrations:

public class DatabaseConfiguration : DbMigrationsConfiguration<MyContext>
{
    public DatabaseConfiguration()
    {
        this.AutomaticMigrationsEnabled = false;
    }
}

I just need to be able to create a new database if one does not exist (so I can quickly delete and recreate a new database for rapid development on my dev machine). However, I don't want to enable automatic migrations because we do migrations in production with 3rd party tools, and EF6 complains if the schema has changed when automatic migrations is enabled.

Any insight or alternative options that address these needs would be greatly appreciated. Thanks!

Best Answer

Maybe you could...

  1. Set the database initializer to null (to disable the check for model compatibility)

    public class MyContext : DbContext
    {
        static MyContext()
        {
            Database.SetInitializer<MyContext>(null);
        }
    }
    
  2. Explicitly call MyContext.Database.CreateIfNotExists() at an appropriate time of your choosing.


Edit

If you're not using Code First Migrations, it looks like you need to remove the DbMigrationsConfiguration:

At run time, Code First looks for a DbMigrationsConfiguration class that’s tied to a context when that context goes through its data­base initialization process. If that class is found, any explicit calls or settings to set database initialization to any of the original three initializers—whose job is to create or drop and create the database—will create the database using the migrations.

https://msdn.microsoft.com/en-us/magazine/dn818489.aspx

I'm thinking it must be doing the same thing even if you don't use an initializer.