Automatically create database in Entity Framework 6 with auto navigation disabled

I would like to be able to automatically create a new database if it doesn't exist using EF6 with the first code, but with automatic navigation disabled.

If I remember correctly, before automatic migrations existed in the Entity Framework, it worked just fine. However, in EF6 I am getting 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: It is not possible to update the database to match the current model because there are pending changes and automatic migration is disabled. Alternatively, record the pending model changes in a code-based transition or enabling automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.

To make sure this exception was not caused by an external factor in my production project, I created a new test project. However, I am getting 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 migration configuration file to disable automatic migration:

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

      

I just need to create a new database if it doesn't exist (so I can quickly drop and recreate the new database for quick development on my dev machine). However, I do not want to enable automatic migration because we are migrating while working with third party tools, and EF6 complains that the schema changed when automatic migration is enabled.

Any ideas or alternatives that address these needs are greatly appreciated. Thank!

+3


source to share


1 answer


Maybe you could ...


Edit



If you are not using First First Migrations, it looks like you need to remove DbMigrationsConfiguration

:

At run time, Code First looks for the DbMigrationsConfiguration class bound to a context when that context goes through the database initialization process. If this class is found, any explicit calls or customizations to set database initialization to any of the three initial initializers, whose job is to create or drop and create a database, will create the database through migration.

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

I think it should do the same even if you are not using an initializer.

+5


source







All Articles