No initial create with Entity Framework migrations

entity-framework migration


I'm trying to get Entity framework migrations working. I've enabled code first migrations, its created a migrations folder, config file and the mig history table, but no initial create. Am i missing a step? This is a new db created by EF (4.3.1).

8/22/2012 9:07:41 AM

Popular Answer

This behavior is not in place by default, but it is available to you easily in many different forms.

  1. You can call context.Database.CreateIfNotExists(); at application startup.

  2. You can use one of the built-in DatabaseInitializers. The CreateDatabaseIfNotExists initializer is built into EntityFramework and just needs to be added to your project.

  3. You could create your own custom database initializer which includes option #1 inside of itself. Example: Code First Migrations and initialization

You can include DatabaseInitializers in your project either by code or via a config file.

Include an EntityFramework Database Initializer via code:

In your application startup you can setup the DatabaseInitializer like so:

System.Data.Entity.Database.SetInitializer<DairyMmmContext>(new System.Data.Entity.CreateDatabaseIfNotExists<DairyMmmContext>());

NOTE: this code has changed multiple times throughout the life of entityframework! This example is for EF 4.3 which is the current production release available via nuget.

Include an EntityFramework Database Initializer via configuration element:

      <context type="MyNamespace.MyEFDataContext, AssemblyName">
          type="System.Data.Entity.CreateDatabaseIfNotExists`2[[MyNamespace.MyEFDataContext, AssemblyName],
               [MyNamespace.Migrations.Configuration,  AssemblyName]], EntityFramework" />

You'll notice this can be a little "ungraceful" with this configuration. You need to replace AssemblyName above with the name of the assembly you keep your entityframework stuff in, replace MyNamespace.MyEFDataContext with the fully qualified name of your entityframework data context, and replace MyNamespace.Migrations.Configuration with the fully qualified name to your configuration class (by default in the Migration folder inside your project).

EDIT: Edited to respond to additional comments

A migration is a change from one schema definition to another schema definition. Creating the empty database is not a migration (but everything after that is). There will be no migration source file in your project for just creating an empty db, that is done in code by the initializer.

If you are already using the DropCreateDatabaseAlways initializer it should be doing that. However, I noticed you are setting the initializer in code which means there is the opportunity for a timing problem (setting the initializer after your context is already past the point of calling any initializers).

You can force entityframework to run your initializer at any point in code with context.Database.Initialize(true); (The parameter is a true/false to force the initialization regardless of the current state). That would drop and recreate your database every time.

But you can also just make sure your initializer is setup as early as possible in your application's life cycle (before you have created a single instance of your context).

5/23/2017 12:02:17 PM

Related Questions


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