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


i able automatically create new database, if not exist, using code-first ef6 automatic migrations disabled.

if recall correctly, before automatic migrations existed in entity framework, worked fine. however, in ef6, receive following exception:

an exception of type 'system.data.entity.migrations.infrastructure.automaticmigrationsdisabledexception' occurred in entityframework.dll not handled in user code

additional information: unable update database match current model because there pending changes , automatic migration disabled. either write pending model changes code-based migration or enable automatic migration. set dbmigrationsconfiguration.automaticmigrationsenabled true enable automatic migration.

to verify exception not caused external factor in production project, created new test project. however, same exception.

for database context, have:

public class mycontext : dbcontext {     public dbset<entity> entities { get; set; }      public mycontext()     {         database.setinitializer(new createdatabaseifnotexists<mycontext>());     } } 

then added database migrations configuration file disable automatic migrations:

public class databaseconfiguration : dbmigrationsconfiguration<mycontext> {     public databaseconfiguration()     {         this.automaticmigrationsenabled = false;     } } 

i need able create new database if 1 not exist (so can delete , recreate new database rapid development on dev machine). however, don't want enable automatic migrations because migrations in production 3rd party tools, , ef6 complains if schema has changed when automatic migrations enabled.

any insight or alternative options address these needs appreciated. thanks!

maybe could...

  1. set database initializer null (to disable check model compatibility)

    public class mycontext : dbcontext {     static mycontext()     {         database.setinitializer<mycontext>(null);     } } 
  2. explicitly call mycontext.database.createifnotexists() @ appropriate time of choosing.


edit

if you're not using code first migrations, looks need remove dbmigrationsconfiguration:

at run time, code first looks dbmigrationsconfiguration class that’s tied context when context goes through data­base initialization process. if class found, explicit calls or settings set database initialization of original 3 initializers—whose job create or drop , create database—will create database using migrations.

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

i'm thinking must doing same thing if don't use initializer.


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -