Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No initial create with Entity Framework migrations

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).

like image 369
newbie_86 Avatar asked Aug 22 '12 09:08

newbie_86


People also ask

How do I add initial migration?

Run the Add-Migration InitialCreate –IgnoreChanges command in Package Manager Console. This creates an empty migration with the current model as a snapshot. Run the Update-Database command in Package Manager Console. This will apply the InitialCreate migration to the database.

How do I enable migrations in Entity Framework?

Open the Package Manager Console from Tools → Library Package Manager → Package Manager Console and then run the enable-migrations command (make sure that the default project is the project where your context class is).


1 Answers

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:

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

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).

like image 59
BenSwayne Avatar answered Sep 30 '22 06:09

BenSwayne