将数据库首先迁移到代码优先 - 从发布切换到迁移 [英] Migrate database-first to code-first - switch from publish to migrations

查看:193
本文介绍了将数据库首先迁移到代码优先 - 从发布切换到迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要将现有项目从数据库优先迁移到代码优先。每个环境(DEV,TEST,PROD)的数据库版本略有不同。差异是DEV的变化可能没有被推到TEST和PROD。

We need to migrate an existing project from database-first to code-first. Each environment (DEV, TEST, PROD) has a slightly different version of the database. The differences are the changes in DEV that may not have been pushed to TEST and PROD.

-

我们正在使用实体框架6.1.3。

We are using Entity Framework 6.1.3.

我们有一个部署数据库的数据库项目。发布操作将数据库与项目进行比较,并构建一个脚本,以根据需要更改数据库。我们有一个具有edmx图的模型项目。我们从数据库更新edmx,并且基于数据库中的表生成实体。

We have a database project that deploys the database. The publish operation compares the database with the project and builds a script to change the database as needed. We have a models project with an edmx diagram. We update the edmx from the database and this generates the entities based on the tables in the database.

我们正在替换 model projec t(与edmx)一起使用代码优先项目数据库项目将会消失,我们的新的代码优先项目将有迁移。

We are replacing the models project (with the edmx) with a code-first project. The database project will go away and our new code-first project will have migrations.

-

我们使用EF PowerTools对数据库进行逆向工程并创建模型。然后我们创建一个迁移,并添加创建数据库所需的语句。但是,当我们更新数据库时,它会崩溃,因为表已经存在。
接下来,我们创建了一个忽略更改的迁移。现在update-database什么也不做。这很好,因为一切都已经存在。但是如果我需要部署到一个新的空数据库,会发生什么?它不会创建任何这些对象。

We used EF PowerTools to reverse engineer the database and create the models. Then we create a migration and it adds the statements needed to create the database. However, when we update-database it crashes because the tables already exist. Next, we created a migration that ignores the changes. Now update-database does nothing. This is fine because everything already exists. But what happens if I need to deploy to a new empty database? It will not create any of these objects.

为了使事情复杂化,DEV中的反向工程师拉入的一些对象不在TEST中。并且TEST中的一些表可能在DEV中发生变化。然后PROD也可能不同。

To complicate matters, some of the objects pulled in from the reverse engineer in DEV are not in TEST. And some tables in TEST may have changed in DEV. And then PROD may also be different.

-

我们如何从数据库首先转换为代码 - 首先还有一个可靠的过程,可以在所有环境中工作,并可以构建一个独立的数据库?

How can we transition from database-first to code-first and still have a reliable process that works in all environments and can build a stand-alone database?

推荐答案

以您的最低基线,可能是PROD。将其附加到您的项目并生成迁移初始或任何。注释掉Up()代码和update-database。附加第二个最新数据库,大概是TEST。再次执行update-database(初始)。添加第二个迁移TestChanges。注释掉Up()代码和update-database。附加DEV数据库。应用第一次迁移(Update-Database -TargetMigration:Initial),应用第二个迁移。现在添加迁移DevChanges,注释掉up代码,update-database。现在,您可以取消注释迁移代码或生成脚本以从以下任意一点获取:

I would start with your lowest baseline, probably PROD. Attach it to your project and generate the migration "Initial" or whatever. Comment out the Up() code and update-database. Attach the second most recent database, probably TEST. Do the update-database again (Initial). Add a second migration "TestChanges". Comment out the Up() code and update-database. Attach the DEV database. Apply the first migration (Update-Database –TargetMigration: Initial), apply the second. Now add-migration "DevChanges", comment out the up code, update-database. Now you can either uncomment the migration code or generate a script to get from any of those points:

Update-Database -Script -SourceMigration: Initial -TargetMigration: DevChanges

我可能会将迁移称为更有意义的东西。

I'd probably call the migrations something more meaningful though.

https:// msdn.microsoft.com/en-us/data/jj591621#specific

这篇关于将数据库首先迁移到代码优先 - 从发布切换到迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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