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):
- Copy EF-project to a staging directory
- Copy migrate.exe in same folder (migrate.exe requires to be placed right next to assembly containing EF migrations)
- Execute migrate.exe
In detail:
- Copy Files "task"
- Source folder: $(build.sourcesdirectory)
- Contents: Contoso.EF\bin\debug\ **
- Target folder: $(build.artifactstagingdirectory)/EF
- 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
- 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