无法生成显式迁移 - 具有多个上下文/配置的Entity Framework 6.1.3

时间:2016-05-18 18:34:34

标签: asp.net asp.net-mvc entity-framework entity-framework-6

我有一个项目作为内部工具集。因此,我需要多个上下文。我有一个上下文设置正常,然后我开始尝试添加我的第二个上下文。我从另一个SO问题中了解到,在启用后需要重命名配置文件,然后在执行Add-Migration和Update-Database命令时引用新的配置文件名。我做到了这一点,结果是"无法生成显式迁移......"其中引用了201605181623556_ServiceRequest,如下所示,已经应用了。

PM> Enable-Migrations -ContextTypeName InsideAdminContext

此时我重命名了配置文件InsideAdminConfiguration

PM> Enable-Migrations -ContextTypeName PartFinderContext

重命名配置文件PartFinderconfiguration

PM> Add-Migration "ServiceRequest" -ConfigurationTypeName InsideAdminConfiguration
PM> update-database -ConfigurationTypeName InsideAdminConfiguration
Applying explicit migrations: [201605181623556_ServiceRequest].
Applying explicit migration: 201605181623556_ServiceRequest.
Running Seed method.
PM> Add-Migration "InitialMigrationForPartFinder" -ConfigurationTypeName PartFinderConfiguration
Unable to generate an explicit migration because the following explicit migrations are pending: [201605181623556_ServiceRequest]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

到目前为止,我已经删除了所有迁移文件夹,删除了表和_MigrationHistory表,并使用上面的命令重新开始,但问题仍然存在。

- - - - - - - - UPDATE
我颠倒了上面的顺序并首先创建了PartFinder表,并在尝试在ServiceRequest上添加迁移时遇到了同样的错误。

1 个答案:

答案 0 :(得分:2)

我在这里找到了解决方案:How do I enable EF migrations for multiple contexts to separate databases?

我失踪的部分是" bart s"的回答。配置文件需要具有自己的命名空间,可以通过Enable-Migrations -MigrationsDirectory参数创建。在我的情况下,它转换为(我在这里有一些额外的参数,由于尝试其他的东西可能没有必要):

Enable-Migrations -MigrationsDirectory "Migrations\InsideAdmin" -ContextTypeName InsideIIMAK.Domain.Concrete.InsideAdminContext -StartUpProjectName InsideAdmin

Enable-Migrations -MigrationsDirectory "Migrations\PartFinder" -ContextTypeName InsideIIMAK.Domain.Concrete.PartFinderContext -StartUpProjectName InsideAdmin

Add-Migration "InitialMigrationForPartFinder" -ConfigurationTypeName InsideIIMAK.Domain.Migrations.PartFinder.PartFinderConfiguration -StartUpProjectName InsideAdmin -ConnectionStringName PartFinderContext

update-database -ConfigurationTypeName InsideIIMAK.Domain.Migrations.PartFinder.PartFinderConfiguration -StartUpProjectName InsideAdmin -ConnectionStringName PartFinderContext

Add-Migration "ServiceRequest" -ConfigurationTypeName InsideIIMAK.Domain.Migrations.InsideAdmin.InsideAdminConfiguration -StartUpProjectName InsideAdmin -ConnectionStringName InsideAdminContext

update-database -ConfigurationTypeName InsideIIMAK.Domain.Migrations.InsideAdmin.InsideAdminConfiguration -StartUpProjectName InsideAdmin -ConnectionStringName InsideAdminContext