在连续部署中签入和发布代码后,如何在Code First方法中自动更新DATABASE [英] How to UPDATE DATABASE in Code First approach automatically after check-in and Publish code in Continuous deployment
问题描述
在我们的Web API应用程序中,连续部署需要以下方案。
In our Web API application, Continuous deployment need a following scenario.
用户将在VS中签入代码,代码将自动生成,代码将被发布,代码将被部署。
User will check in code in VS, Code will get automatically build, Code will be Published, Code will be deployed.
但是,如果我们使用的是Entity Framework Code First方法,如何在没有手动命令的情况下更新数据库(添加-迁移/更新数据库)并进行该签入数据库是最新的。
But If we are using Entity Framework Code First approach, How can we update database without manual commands (Add-Migration/Update Database)and make database up to date with that check-in.
推荐答案
您可以尝试在构建/部署过程中运行添加-迁移/更新数据库命令。
You can try to run Add-Migration/Update Database commands in the build/deploy process.
假设您使用的是vNext构建,
Assume you are using vNext build,
- 添加 <$ c构建定义中的 $ c> Nuget Installer 任务首先使
在构建过程中恢复实体框架。Migrate.exe
将
安装在\packages\EntityFramework.\tools
文件夹中。 / li>
- 然后添加
命令行
任务以运行migration.exe。在工具 $ c $中输入
\packages\EntityFramework.\tools\migrate.exe
c>区域和
参数
字段中的参数。
- Add a "
Nuget Installer
" task in your build definition first to restore the Entity Framework during the build.Migrate.exe
will be installed in\packages\EntityFramework.\tools
folder. - Then add a "
Command Line
" task to run the migrate.exe. Enter"\packages\EntityFramework.\tools\migrate.exe"
in"Tool"
area and the arguments in"Arguments"
field.
参考此线程:如何我是从Visual Studio Online运行Entity Framework的migration.exe吗?
您还可以尝试使用扩展名 实体框架迁移,其中包含一组任务,可让您使用实体框架代码的首次迁移:
You can also try the extension "Entity Framework Migrations" which contains a set of tasks which allow you to work with Entity Framework code first migrations:
方法1:生成SQL脚本
Method 1: Generating SQL script
第一种方法允许您生成包含所有$的SQL脚本。 b $ b迁移。可以通过手动运行
Update-Database -SourceMigration 0 -Script in Visual Studio中的NuGet包
管理器控制台。 然后,您可以在发布后手动运行
该脚本,也可以在发布
期间使用允许您运行SQL脚本的扩展名自动运行该脚本。
The first method allows you to generate a SQL script containing all migrations. This script can be obtained by manually running Update-Database -SourceMigration 0 -Script in the NuGet package manager console in Visual Studio. You can then either manually run this script after the release or automatically during the release using a extension that allows you to run SQL scripts.
任务名称:生成迁移SQL脚本
Task name: Generate migration SQL script
其他文章可能会有所帮助:
Other articles may helps:
- Deploying an Entity Framework Database into Production
- Using TFS Build to Deploy Entity Framework Database Migrations with Migrate.exe
- Continuous Delivery with TFS: Our Sample Application
这篇关于在连续部署中签入和发布代码后,如何在Code First方法中自动更新DATABASE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!