如何为多个上下文启用 EF 迁移到单独的数据库?

编程入门 行业动态 更新时间:2024-10-25 04:14:28
本文介绍了如何为多个上下文启用 EF 迁移到单独的数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何为同一项目中的多个数据库上下文启用实体框架 5(版本 5.0.0)迁移,其中每个上下文对应于自己的数据库?当我在 PM 控制台 (Visual Studio 2012) 中运行 Enable-Migrations 时,由于存在多个上下文而出现错误:

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.

如果我运行 Enable-Migrations -ContextTypeName DatabaseService.Models.Product1DbContext 我不允许运行 Enable-Migrations -ContextTypeName DatabaseService.Models.Product2DbContext 因为迁移已经存在:已在项目DatabaseService"中启用迁移.要覆盖现有的迁移配置,请使用 -Force 参数.

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.

推荐答案

第二次调用 Enable-Migrations 失败,因为 Configuration.cs 文件已经存在.如果您重命名该类和文件,您应该能够运行第二个 Enable-Migrations,这将创建另一个 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 迁移到单独的数据库?

本文发布于:2023-05-26 16:22:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/265235.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   上下文   何为   数据库   EF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!