Automatically Create Database in Entity Framework 6 with Automatic Migrations Disabled

entity-framework entity-framework-6

Question

If a database does not already exist, I would like to be able to automatically establish one using code-first EF6, but without enabling automated migrations.

If I remember correctly, this worked perfectly before Entity Framework had automatic migrations. However, I get the following exception with EF6:

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.

I made a new test project to make sure this exception in my production project wasn't brought on by something external. I encounter the same exception, though.

In the context of my database, I have:

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

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

Then, in order to stop automatic migrations, I added a database migrations configuration file:

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

If a database is not already there, I just need to be able to create one (so I can quickly delete and recreate a new database for rapid development on my dev machine). The reason I don't want to enable automatic migrations is that we utilize third-party tools to perform migrations in production, and when automatic migrations are enabled, EF6 complains if the schema has changed.

We would be grateful for any advice or creative solutions that address these needs. Thanks!

1
3
7/14/2015 2:42:47 PM

Accepted Answer

You could possibly...

  1. Initialize the database with a value of 0 (to disable the check for model compatibility)

    public class MyContext : DbContext
    {
        static MyContext()
        {
            Database.SetInitializer<MyContext>(null);
        }
    }
    
  2. Make a direct call to MyContext.Database.CreateIfNotExists() at your convenience and the proper time.


Edit

It appears that you must get rid of the if you aren't using Code First Migrations.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

If you don't use an initializer, I assume it still does the same thing.

5
7/14/2015 10:45:25 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow