How to UPDATE DATABASE in Code First approach automatically after check-in and Publish code in Continuous deployment

会有一股神秘感。 提交于 2019-12-23 03:19:57

问题


​In our Web API application, Continuous deployment need a following scenario.

User will check in code in VS, Code will get automatically build, Code will be Published, Code will be deployed.

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.


回答1:


You can try to run Add-Migration/Update Database commands in the build/deploy process.

Assume you are using vNext build,

  1. 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.
  2. 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.

Reference this thread : How can I run Entity Framework's migrate.exe from Visual Studio Online?

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:

Method 1: Generating SQL script

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.

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


来源:https://stackoverflow.com/questions/46280237/how-to-update-database-in-code-first-approach-automatically-after-check-in-and-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!