How do I enable Entity Framework 5 (version 5.0.0) migrations for multiple DB contexts in the same project, where each context corresponds to its own database? When I run Enable-Migrations
in the PM console (Visual Studio 2012), there's an error because of there being multiple contexts:
PM> Enable-Migrations
More than one context type was found in the assembly 'DatabaseService'.
To enable migrations for DatabaseService.Models.Product1DbContext, use Enable-Migrations -ContextTypeName DatabaseService.Models.Product1DbContext.
To enable migrations for DatabaseService.Models.Product2DbContext, use Enable-Migrations -ContextTypeName DatabaseService.Models.Product2DbContext.
If I run Enable-Migrations -ContextTypeName DatabaseService.Models.Product1DbContext
I'm not allowed to run Enable-Migrations -ContextTypeName DatabaseService.Models.Product2DbContext
because a migration already exists: Migrations have already been enabled in project 'DatabaseService'. To overwrite the existing migrations configuration, use the -Force parameter.
In addition to what @ckal suggested, it is critical to give each renamed Configuration.cs its own namespace. If you do not, EF will attempt to apply migrations to the wrong context.
Here are the specific steps that work well for me.
If Migrations are messed up and you want to create a new "baseline":
Creating the initial migration:
In Package Manager Console:
In Solution Explorer: Rename Migrations.Configuration.cs to Migrations.ConfigurationA.cs. This should automatically rename the constructor if using Visual Studio. Make sure it does. Edit ConfigurationA.cs: Change the namespace to NamespaceOfContext.Migrations.MigrationsA
In Solution Explorer: Rename Migrations.Configuration.cs to Migrations.ConfigurationB.cs. Again, make sure the constructor is also renamed appropriately. Edit ConfigurationB.cs: Change the namespace to NamespaceOfContext.Migrations.MigrationsB
Steps to create migration scripts in Package Manager Console:
Run command
or -
It is OK to re-run this command until changes are applied to the DB.
Either run the scripts against the desired local database, or run Update-Database without -Script to apply locally:
or -
I just bumped into the same problem and I used the following solution (all from Package Manager Console)
This will create 2 separate folders in the Migrations folder. Each will contain the generated
Configuration.cs
file. Unfortunately you still have to rename thoseConfiguration.cs
files otherwise there will be complaints about having two of them. I renamed my files toConfigA.cs
andConfigB.cs
EDIT: (courtesy Kevin McPheat) Remember when renaming the Configuration.cs files, also rename the class names and constructors /EDIT
With this structure you can simply do
Which will create the code files for the migration inside the folder next to the config files (this is nice to keep those files together)
And last but not least those two commands will apply the correct migrations to their corrseponding databases.
EDIT 08 Feb, 2016: I have done a little testing with EF7 version 7.0.0-rc1-16348
I could not get the -o|--outputDir option to work. It kept on giving
Microsoft.Dnx.Runtime.Common.Commandline.CommandParsingException: Unrecognized command or argument
However it looks like the first time an migration is added it is added into the Migrations folder, and a subsequent migration for another context is automatically put into a subdolder of migrations.
The original names
ContextA
seems to violate some naming conventions so I now useContextAContext
andContextBContext
. Using these names you could use the following commands: (note that my dnx still works from the package manager console and I do not like to open a separate CMD window to do migrations)This will create a model snapshot and a initial migration in the
Migrations
folder forContextAContext
. It will create a folder namedContextB
containing these files forContextBContext
I manually added a
ContextA
folder and moved the migration files fromContextAContext
into that folder. Then I renamed the namespace inside those files (snapshot file, initial migration and note that there is a third file under the initial migration file ... designer.cs). I had to add.ContextA
to the namespace, and from there the framework handles it automatically again.Using the following commands would create a new migration for each context
and the generated files are put in the correct folders.
The 2nd call to Enable-Migrations is failing because the Configuration.cs file already exists. If you rename that class and file, you should be able to run that 2nd Enable-Migrations, which will create another Configuration.cs.
You will then need to specify which configuration you want to use when updating the databases.
In case you already have a "Configuration" with many migrations and want to keep this as is, you can always create a new "Configuration" class, give it another name, like
then just issue the command
and EF will scaffold the migration without problems. Finally update your database, from now on, EF will complain if you don't tell him which configuration you want to update:
Done.
You don't need to deal with Enable-Migrations as it will complain "Configuration" already exists, and renaming your existing Configuration class will bring issues to the migration history.
You can target different databases, or the same one, all configurations will share the __MigrationHistory table nicely.