DevPerson
DevPerson

Reputation: 419

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

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

Upvotes: 1

Views: 3349

Answers (1)

Andy Li-MSFT
Andy Li-MSFT

Reputation: 30442

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:

Upvotes: 3

Related Questions