I just bumped into the same problem and I used the following solution (all from Package Manager Console)
PM> Enable-Migrations -MigrationsDirectory "Migrations\ContextA" -ContextTypeName MyProject.Models.ContextA
PM> Enable-Migrations -MigrationsDirectory "Migrations\ContextB" -ContextTypeName MyProject.Models.ContextB
This will create 2 separate folders in the Migrations folder. Each will contain the generated Configuration.cs
file. Unfortunately you still have to rename those Configuration.cs
files otherwise there will be complaints about having two of them. I renamed my files to ConfigA.cs
and ConfigB.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
PM> Add-Migration -ConfigurationTypeName ConfigA
PM> Add-Migration -ConfigurationTypeName ConfigB
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)
PM> Update-Database -ConfigurationTypeName ConfigA
PM> Update-Database -ConfigurationTypeName ConfigB
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 use ContextAContext
and ContextBContext
. 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)
PM> dnx ef migrations add Initial -c "ContextAContext"
PM> dnx ef migrations add Initial -c "ContextBContext"
This will create a model snapshot and a initial migration in the Migrations
folder for ContextAContext
. It will create a folder named ContextB
containing these files for ContextBContext
I manually added a ContextA
folder and moved the migration files from ContextAContext
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
PM> dnx ef migrations add Update1 -c "ContextAContext"
PM> dnx ef migrations add Update1 -c "ContextBContext"
and the generated files are put in the correct folders.