The Self-Taught Programmer

Recipes from real-world experience with asp.net.

How to Enable and Use EF Code-First Migrations for Multiple Contexts

with 4 comments

The Situation

You want to use Entity Frameworks code-first migrations to push model changes to the database, but you have multiple databases and contexts in your model. When you try to run Enable-Migrations for the second context, you get an error that looks like this:

Migrations have already been enabled in project 'Project1'. To overwrite the 
existing migrations configuration, use the -Force parameter.

If you go ahead and use the “-force” parameter, it will overwrite your Configuration.cs file for the 1st context, which isn’t what you want.

A Solution

The work-around for this is to rename the Configuration.cs file that is generated on the first run and to give it its own namespace. Here are the steps I took:

1) I keep my data access code separate from my UI by putting it in a separate project and referencing it from my UI project. To make Enable-Migrations work properly, I had to temporarily make my data access project my startup project by right-clicking on the project in the Solution Explorer and clicking on “Set as startup project”. (You won’t need to do this if your contexts are in the same project as the rest of your application.)

2) In the Package Manager Console, run this command:

Enable-Migrations -EnableAutomaticMigrations -ContextTypeName NameOfFirstContext

3) A Migrations folder will be added to your project with a Configuration.cs file in it. Rename the file. Maybe something like “ConfigurationContext1.cs”.

4) Now open the Configuration.cs file and change the namespace. I added the name of the context to the namespace.

This is important. Giving each configuration file its own namespace will make sure that migrations are only applied to the correct context. If you don’t do this, the Update-Database command will look at ALL the pending migrations and try to apply them to the current context. This could result in an error (such as “Unable to generate an explicit migration because the following explicit migrations are pending:“) or tables might be added to/deleted from the wrong context/database.

5) In the Package Manager Console, run the same command for for the next context:

Enable-Migrations -EnableAutomaticMigrations -ContextTypeName NameOfSecondContext

6) Repeat the above steps for each context that you need to enable migrations.

7) Rename the new Configuration.cs file, maybe “ConfigurationContext2.cs”.

To update the database with changes to your model, simply specify the configuration file to use for the update like this:

Update-Database -ConfigurationTypeName ConfigurationContext2

About these ads

Written by Jim McMullen

February 14, 2013 at 11:14 am

Posted in How-To

Tagged with

4 Responses

Subscribe to comments with RSS.

  1. I have not tried this yet but it appears to be just what I need. Thanks for the write up!!

    Sam

    September 17, 2013 at 5:20 pm

    • Thank you so very much! After a fair amount of searching the web, the part about renaming the namespace was the thing I was missing.

      Mike

      November 26, 2013 at 8:39 pm

  2. Thank you ,it worked after changing the namespace.

    ravihanok

    December 14, 2013 at 1:33 am

  3. […] have separate Migrations set up for the various contexts using this guide How to Enable and Use EF Code-First Migrations for Multiple Contexts. This worked flawlessly, until I attempted to Update-Database on Component1Context. The results […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: