3
votes

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
What have you tried? What research have you done? What hasn't worked?Daniel Mann
Can't use dotnet ef commands in the command line, because it's not a core project. I can't find anything online about running someone equivalent to package manager console on the microsoft hosted vm. Things like migrations.exe don't work because they don't generate a script.Ashley Edds
@Ashley Edds Not get your latest information, is the workaround helpful for you? Or if you have any concern, feel free to share it here.Hugh Lin

2 Answers

0
votes

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.

enter image description here

In order to this Task, you'll have to take the following steps:

  1. Build your project to an output folder and include the migrate.exe executable that comes with Entity Framework 6.
  2. Create an automated build that packages up your files and makes them accessible during a Release.
  3. Create a Release definition for the relevant Build
  4. 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.
0
votes

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.