与 EF Code First 迁移持续集成 [英] Continuous Integration with EF Code First Migrations

查看:13
本文介绍了与 EF Code First 迁移持续集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我是否可以完全自动化代码优先迁移以实现持续集成.

I was wondering if I could automate completely code first migrations for continuous integration.

目前我的持续集成只是简单地更新代码更改,但是,我手动生成迁移,并更新我的持续集成服务器上的数据库.

Currently my continuous integration simply simply updates the code changes, however, I manually generate a migration, and update the database on my continuous integration server.

生成迁移并自动更新数据库是否可靠/可能/推荐?

Is it reliable / possible / recommended to generate the migrations and update the database automatically?

例如:

我的用户具有属性 userId 和用户名.然后我将属性年龄添加到代码中.当前方案需要我创建一个迁移来捕获此更改,然后我将我的更改签入到版本控制中.持续集成将发现这种变化,并将部署新版本.我必须手动更新数据库(应该是自动化的).

I have user with property userId and username. I then add a property age into the code. Current scenario would require me to create a migration that will capture this change, and then I check in my changes to the version control. The continuous integration will spot this change, and will deploy the new version. I have to manually update-database (which should be automated).

我是否也可以跳过迁移的生成,这样我可以简单地将属性年龄添加到代码中,持续集成将生成此迁移.不确定这是否被推荐.

Can I skip out the generation of migration too, such that I can simply add the property age onto the code, and the continuous integration will generate this migration. Not sure if this is recommended.

推荐答案

答案是肯定的,但不是你描述的那样.

The answer is yes, but not quite how you're describing.

您必须并且应该手动生成迁移.并非所有迁移都可以自动创建,在这些情况下,需要手动修改生成的迁移.列拆分、某些类型的数据类型更改等.

You must and should manually generate the migration. Not all migrations can be created automatically, and in those cases, manually modification of the generated migration is necessary. Column splits, certain types of data type changes, etc.

然后,您的 CI 服务器可以利用 migrate.exe 使您的数据库与您的模型同步.棘手的部分是处理导致降级的迁移.所以从 v1 到 v2 很容易,但是从 v2 回到 v1 比较棘手,因为只有 v2 程序集知道如何回到"到 v1.

Your CI server can then leverage migrate.exe to get your databases synced up with your model. The tricky part is handling migrations that result in downgrades. So going from v1 to v2 is easy, but from v2 back to v1 is trickier as only the v2 assembly knows how to get "back" to v1.

我最终创建了一个自定义工具,它可以智能地执行迁移并自动确定要用于迁移的模型(上下文)程序集.您可以在此处了解如何执行此操作:EF Code First Migrations部署旧版本

I ended up creating a custom tool that executed migrations intelligently and automatically determined which model (context) assembly to use for the migration. You can get an idea of how to do that here: EF Code First Migrations to Deploy Older Version

最终结果是我可以签入模型更改/迁移,并且知道我的数据库更改将自动部署到属于我的 ci/cd 管道的任何环境 - 是的,这绝对包括生产.

The end result is that I can check in a model change / migration and know that my db change will be deployed automatically to any environment that's part of my ci/cd pipeline - and yes, that absolutely includes production.

这篇关于与 EF Code First 迁移持续集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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