I have an EF6 project that I would like to generate a script for migrations. Locally, I just run update-database - script
, but I would like to create a build pipeline that generates a script for this, and later a deployment pipeline that executes this script when we are ready to push to different environments. Is there anyway to generate such a script in an Azure Devops Build Pipeline?
2 Answers
For this issue , you can try Entity Framework Migration Extensions.
This task allows a Build / Release to provide database connection parameters and execute an Entity Framework 6 migration against the database.
In order to this Task, you'll have to take the following steps:
- Build your project to an output folder and include the migrate.exe executable that comes with Entity Framework 6.
- Create an automated build that packages up your files and makes them accessible during a Release.
- Create a Release definition for the relevant Build
- Add an EF6 Migration task. Once that task is added to an environment within the release, you'll need to enter the appropriate parameters to configure it. All file path parameters should be within the file system for the build, none of them are for TFS source control paths.
I managed to run an EF6 Migration in my Release Pipeline by first copying the entire Project from the Build Pipeline into the Artifact Staging Directory with a Copy Files Task:
Source Folder: $(build.sourcedirectory)\<SolutionName>\
Target Folder: $(build.artifactstagingdirectory)\EFMigration
and then running the script that is executed by the "update-database" command in a Command Line Task. I got the script by manually running the "update-database -verbose" command in the Package Manager Console of the EF6 Project, copying the shown script and adjusting the paths to the ones I copied into the Artifact Staging Directory. It should look something like this:
$(System.DefaultWorkingDirectory)/<..>/drop/EFMigration/packages/EntityFramework.6.4.4/tools/net45/any/ef6.exe database update --no-color --prefix-output --assembly $(System.DefaultWorkingDirectory)/<..>/drop/EFMigration/EFM.Data/bin/Release/EFM.Data.dll --project-dir $(System.DefaultWorkingDirectory)/<..>/drop/EFMigration/EFM.Data\ --language C# --data-dir $(System.DefaultWorkingDirectory)/<..>/drop/EFMigration/EFMigration/App_Data --root-namespace EFM.Data --config $(System.DefaultWorkingDirectory)/<..>/drop/EFMigration/EFMigration/Web.config
In my case the "Add-Migration" command will be run manually before commiting and the created scaffold will automatically be used in the Release Pipeline. You could use the same method I used to get the "Update-Database" script to get the "Add-Migration" script but I have not tried automating this.