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

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

Advertisement

4 thoughts on “How to Enable and Use EF Code-First Migrations for Multiple Contexts

  1. Pingback: In Multiple Context scenario, every DbContext wants to generate Identity* Tables with Migrations - Tech Forum Network

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 )

Facebook photo

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

Connecting to %s