Multiple Database / DbContext Migration with EF Code First

code-first ef-migrations entity-framework multiple-databases

Question

Each of my two databases has its own dbcontext. Two migration setups have been put up. I have no problems creating a migration for the first database.Add-Migration DB1_InitialCreate -ConfigurationTypeName DB1Configuration When I use the second database to generate an initial migration, I attempt toAdd-Migration DB2_InitialCreate -ConfigurationTypeName DB2Configuration I encounter the following error:

Unable to generate an explicit migration because the following explicit migrations are pending: [201205082215265_DB1_InitialCreate]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

So I update the database as instructed and add:

Update-Database -ConfigurationTypeName DB1Configuration

I attempt to create a migration for the second database once more after that, but I still receiving the same issue.

Any suggestions on how to make migrations for two databases/contexts work?

1
8
10/6/2015 3:27:09 PM

Accepted Answer

I was able to provide a response to my own inquiry. In the same namespace, I had two configuration classes. I finally got them apart and everything worked.

11
5/9/2012 5:50:48 PM

Popular Answer

The Instance Model 6 makes it simple.

The process is the same whether there are several DbContexts for a single database or numerous DbContexts for separate databases:

Enable-Migrations -ContextTypeName <DbContext-Name-with-Namespaces> -MigrationsDirectory:<Migrations-Directory-Name>

Add-Migration -configuration <DbContext-Migrations-Configuration-Class-with-Namespaces> <Migrations-Name>

Update-Database -configuration <DbContext-Migrations-Configuration-Class-with-Namespaces> -Verbose

Source



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow