8

I have two database each with their own dbcontext. I've setup two migration configurations. I can add a migration for the first db ust fine (Add-Migration DB1_InitialCreate -ConfigurationTypeName DB1Configuration). When I try to create an initial migration with the second db using: Add-Migration DB2_InitialCreate -ConfigurationTypeName DB2Configuration, I get 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 do what it says and update the database with:

Update-Database -ConfigurationTypeName DB1Configuration

Then I try to add a migration again for the second db but I keep getting the same error.

Any ideas on how to get migrations working for two databases/contexts?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
seano288
  • 191
  • 1
  • 1
  • 7

3 Answers3

11

I've been able to answer my own question. My two configuration classes existed in the same namespace. As soon as I separated them, everything worked.

seano288
  • 191
  • 1
  • 1
  • 7
  • I have two DbContexts in the same namespace, but don't get this problem. Using EF 5.0 RTM. – angularsen Oct 23 '12 at 16:44
  • 3
    I also had to separate them by namespace (EF 6.x); it wasn't enough to Update-Database -ConfigurationTypeName nameOfConfiguration – subsci Nov 04 '14 at 00:03
7

With Entity Framework 6 it's easy.

It's the same procedure for both multiple DbContexts for one database and for multiple DbContexts for each their own database:

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

LJNielsenDk
  • 1,414
  • 1
  • 16
  • 32
5

It's been a while, but this could be a better answer to your problem :)

Update-Database -ConfigurationTypeName "SlaveConfiguration"
                -StartupProjectName "FacturatieMVCv2.Data" -Verbose 
                -ConnectionString "connstring;" 
                -ConnectionProviderName "System.Data.SqlClient"
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
NicoJuicy
  • 3,435
  • 4
  • 40
  • 66