How can I run Entity Framework's migrate.exe f

2019-01-19 03:14发布

I have set up continuous integration for my project with Visual Studio Online build definitions.

When it comes to deploying my database (to an Azure test environment) I just build my SQL Server Database Project with the right publishing settings.

But I want to switch to Entity Framework's code first approach and leverage the migration feature, which requires me to call migrate.exe.

My question is - how could I run migrate.exe from VSO build definitions?

3条回答
ゆ 、 Hurt°
2楼-- · 2019-01-19 03:51

You can also look at executing your migrations at App startup time.

add the following to your Application_Start() event in global.asax

var configuration = new Configuration();
var migrator = new DbMigrator(configuration);
migrator.Update();

This will fire the migrations at each application startup. you could also wrap with conditional logic to control how it is fired.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-19 03:54

We've succesfully implemented an automated EF code first migration at deploy time on top of TFS Build vNext in the following way:

It basically involves 3 steps (per EF-context):

  1. Copy EF-project to a staging directory
  2. Copy migrate.exe in same folder (migrate.exe requires to be placed right next to assembly containing EF migrations)
  3. Execute migrate.exe

In detail:

  1. Copy Files "task"
    • Source folder: $(build.sourcesdirectory)
    • Contents: Contoso.EF\bin\debug\ **
    • Target folder: $(build.artifactstagingdirectory)/EF
  2. Copy Files "task"
    • Source folder: $(build.sourcesDirectory)\packages\EntityFramework.6.1.3\tools
    • Contents: migrate.exe
    • Target folder: $(build.artifactstagingdirectory)\EF\Contoso.EF\bin\debug\bin\debug
  3. Batch script "task"
    • Path: $(build.sourcesdirectory)_Deploy\MigrateEFContext.bat
    • Arguments: $(build.artifactstagingdirectory)\EF\Contoso.EF\bin\debug Contoso.EF.dll [SQL-SERVER-INSTANCE] [DbName] System.Data.SqlClient

The MigrateEFContext.bat file assembles the migrate.exe-command with its arguments:

SET EFDir=%1
SET EFContext=%2
SET connStringDataSource=%3
SET connStringInitialCatalog=%4
SET connectionProviderName=%5

%EFDIR%\migrate.exe %EFContext% /ConnectionString:"Data Source=%connStringDataSource%;Initial Catalog=%connStringInitialCatalog%;Integrated Security=true" /connectionProviderName:%connectionProviderName% /verbose
查看更多
Ridiculous、
4楼-- · 2019-01-19 04:04

I assume you are using vNext build, 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.

查看更多
登录 后发表回答