如何启用EF迁移多个上下文来单独的数据库? [英] How do I enable EF migrations for multiple contexts to separate databases?
问题描述
如何启用实体框架5(5.0.0版)迁移在同一个项目中,其中每个方面对应于它自己的数据库多个数据库环境?当我运行启用-迁移的PM控制台(的Visual Studio 2012)
,还有是因为那里是多种环境中的一个错误:
How do I enable Entity Framework 5 (version 5.0.0) migrations for multiple DB contexts in the same project, where each context corresponds to its own database? When I run Enable-Migrations
in the PM console (Visual Studio 2012), there's an error because of there being multiple contexts:
PM> Enable-Migrations
More than one context type was found in the assembly 'DatabaseService'.
To enable migrations for DatabaseService.Models.Product1DbContext, use Enable-Migrations -ContextTypeName DatabaseService.Models.Product1DbContext.
To enable migrations for DatabaseService.Models.Product2DbContext, use Enable-Migrations -ContextTypeName DatabaseService.Models.Product2DbContext.
如果我跑启用-迁移-ContextTypeName DatabaseService.Models.Product1DbContext
我不允许运行启用-迁移-ContextTypeName DatabaseService.Models .Product2DbContext
,因为迁移已存在:迁移已经在项目中启用DatabaseService。要覆盖现有的迁移配置,使用强制参数。
If I run Enable-Migrations -ContextTypeName DatabaseService.Models.Product1DbContext
I'm not allowed to run Enable-Migrations -ContextTypeName DatabaseService.Models.Product2DbContext
because a migration already exists: Migrations have already been enabled in project 'DatabaseService'. To overwrite the existing migrations configuration, use the -Force parameter.
推荐答案
第二个呼叫启用-迁移失败,因为Configuration.cs文件已经存在。如果您重命名类和文件,你应该能够运行第二个启用,迁移,这将创造另一个Configuration.cs。
The 2nd call to Enable-Migrations is failing because the Configuration.cs file already exists. If you rename that class and file, you should be able to run that 2nd Enable-Migrations, which will create another Configuration.cs.
您一定要去指定要在更新数据库时要使用的配置。
You will then need to specify which configuration you want to use when updating the databases.
Update-Database -ConfigurationTypeName MyRenamedConfiguration
这篇关于如何启用EF迁移多个上下文来单独的数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!