与EF代码首次迁移持续整合 [英] Continuous Integration with EF Code First Migrations

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

问题描述

我想知道是否可以自动完成代码首次迁移以实现持续集成。

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).

我可以跳过迁移的生成,以便我可以简单地将属性age添加到代码中,并且持续集成将生成此迁移。不确定是否推荐。

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代码首次迁移部署旧版本

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代码首次迁移持续整合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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