Search code examples
entity-frameworkazure-devopsdatabase-migrationazure-devops-pipelines

Managing EF migrations in Azure DevOps Pipeline


So our team has implemented a Windows self-hosted Azure DevOps testing pipeline. It was working well until we ran the backend tests for a branch that had a database migration. Although the tests passed as expect, this migration changed the agent's database. This resulted in every subsequent integration test run on the master branch to fail. Normally it would just 'down' the migration and it would be fine but here that migration doesn't exist since it was in a branch that hasn't been merged into master yet.

I've looked at a bunch of other posts about EF migrations and they all seem to talk about release pipelines and creating new migrations. All I want to do is have a step at the end of my pipeline that can update the database to whatever master's most recent migration is. However, I can't figure out how to do that since I don't have access to the NuGet Packet Manager Console. Does anyone know how I might go about updating the database to the appropriate migration? I am also pretty new to Azure DevOps pipelines so please let me know if this is misguided for whatever reason. Thank you for your help and please let me know if I can provide any more information.

If it is any help this is what we would use to update the database based on a given target migration in the NuGet Packet Manager Console:

Update-Database -projectName <projectName> -targetMigration <targetMigration> -configurationTypeName <configurationType> -connectionStringName <connectionString>

Solution

  • So I guess the big problem I was running into is that my project is using EF 6 but most of the solutions online want you to use dotnet commands. To my knowledge (please let me know if I am mistaken) EF 6 can't use dotnet commands.

    To get around this I used ef6.exe which allows you to make migrations via the command line. In the pipeline I just move that ef6.exe to where my project assembly file is and execute the migration. This worked for me but for anyone using EF Core, PatrickLu-MSFT's solution should work and be more straight forward.

    Even though it was changed in EF 6.3 from migrate.exe it doesn't look like Microsoft has updated the docs for ef6.exe. To get an idea of what it is you can look at the out of date migrate.exe documentation and then the conversion table that was posted as a github comment.

    Conversion table from github comment linked above

    Conversion table: