实体框架6(EF6)使用单独项目中的模型进行首次迁移 [英] Entity Framework 6 (EF6) code first migrations with models in separate project

查看:128
本文介绍了实体框架6(EF6)使用单独项目中的模型进行首次迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用EF6代码优先迁移,我可以成功保存模型并为其创建新的迁移。然而,我的DbContext类是一个 Sharp.Data 项目,实际(Sql CE)数据库生活在 Sharp.Server project bin文件夹,我的模型生活在一个 Sharp.Common 项目中。

Using EF6 code-first migrations, I am able to successfully save models and create new migrations for them. However, my DbContext class is in a Sharp.Data project, the actual (Sql CE) database lives under the Sharp.Server project bin folder, and my models live in a Sharp.Common project.

当我运行 add-migration -ProjectName Sharp.Data Migration3 (指向 Sharp.Data ,因为这是DbContext的地方),它成功运行并识别在 Sharp.Common 项目中对模型所做的更改。但是,当我运行 update-database -ProjectName Sharp.Data 时,它会更新/创建/迁移/种子一个位于我的 Sharp.Data 项目,而不是 Sharp.Server ,最终应用程序从中读取数据。

When I run add-migration -ProjectName Sharp.Data Migration3 (pointing to Sharp.Data as that is where the DbContext is), it successfully runs and identifies changes made to the models in the Sharp.Common project. However, when I run update-database -ProjectName Sharp.Data, it updates/creates/migrates/seeds a new database located under the bin folder of my Sharp.Data project, instead of Sharp.Server, where ultimately the application reads the data from.

有没有办法呢?要迁移生成/更新存在于其他地方的数据库,除了自己的项目文件?这可能是使用SQL CE的遗骸,但是由于各种原因,这是我的数据库。

Is there a way to do this? To have migrations generate/update a database that exists somewhere else besides its own project file? This may be a relic of using SQL CE, but it's my database of choice for various reasons.

推荐答案

您可以传递额外的参数到 update-database ,允许:

You can pass extra parameters to update-database that allow to:


  • 指定不同的连接字符串

  • 生成一个可以自己应用到目标数据库的SQL脚本

完整语法更新数据库

Update-Database [-SourceMigration <String>] [-TargetMigration <String>]
[-Script] [-Force] [-ProjectName <String>] [-StartUpProjectName <String>] 
[-ConfigurationTypeName <String>] [-ConnectionStringName <String>] 
[<CommonParameters>]

Update-Database [-SourceMigration <String>] [-TargetMigration <String>] 
[-Script] [-Force] [-ProjectName <String>] [-StartUpProjectName <String>] 
[-ConfigurationTypeName <String>] -ConnectionString <String> 
-ConnectionProviderName <String> [<CommonParameters>]

指定连接字符串(指向项目中正确的文件夹) ):

To specify a connection string (which points to the correct folder in your project):


  • -StartUpProjectName -ConnectionStringName ,以指定包含.config文件的项目,其中带有提供名称的连接字符串(第一个语法)

  • -ConnectionString ,直接指定连接字符串(第二个语法)

  • -StartUpProjectName and -ConnectionStringName, to specify a project that contains a .config file with a connection string with the provided name (1st syntax)
  • -ConnectionString, to specify directly the connection string (2nd syntax)

要创建一个SQL脚本,您可以直接应用于DB使用其他工具:

To create a SQL script that you can apply directly to the DB using other tool:


  • -SourceMigration :目标数据库中的当前迁移 -TargetMigration :迁移到更新到,和 -Script :生成一个可以手动应用的脚本taht

  • -SourceMigration: current migration in the destination DB -TargetMigration: migration to update to, and -Script: to generate a script taht you can apply by hand

这篇关于实体框架6(EF6)使用单独项目中的模型进行首次迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆