简体   繁体   English

具有多个数据库/ DbContext的EF代码优先迁移

[英]EF Code First Migration with Multiple Database / DbContext

I have two database each with their own dbcontext. 我有两个数据库,每个数据库都有自己的dbcontext。 I've setup two migration configurations. 我设置了两个迁移配置。 I can add a migration for the first db ust fine ( Add-Migration DB1_InitialCreate -ConfigurationTypeName DB1Configuration ). 我可以为第一个db ust罚款Add-Migration DB1_InitialCreate -ConfigurationTypeName DB1ConfigurationAdd-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: 当我尝试使用第二个db创建初始迁移时: Add-Migration DB2_InitialCreate -ConfigurationTypeName DB2Configuration ,我收到以下错误:

Unable to generate an explicit migration because the following explicit migrations are pending: [201205082215265_DB1_InitialCreate]. 由于以下显式迁移未决,因此无法生成显式迁移:[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? 有关如何使迁移适用于两个数据库/上下文的任何想法?

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. 我把它们分开后,一切都运转了。

With Entity Framework 6 it's easy. 使用Entity Framework 6很容易。

It's the same procedure for both multiple DbContexts for one database and for multiple DbContexts for each their own database: 对于一个数据库,多个DbContexts和每个自己的数据库的多个DbContexts都是相同的过程:

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 资源

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM