Managing EF migrations in Azure DevOps Pipeline

6.1k Views Asked by At

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>
3

There are 3 best solutions below

3
On BEST ANSWER

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:

0
On

According to documentation Update-Database you should use -Migration paramater:

The target migration. Migrations may be identified by name or by ID. The number 0 is a special case that means before the first migration and causes all migrations to be reverted. If no migration is specified, the command defaults to the last migration.

So it would be

Update-Database -Project <projectName> -Migration <targetMigration> -Connection <connectionString>

or using dotnet tool

dotnet ef database update <targetMigration>
3
On

Not sure if totally get your point, you could take a look at this article-- Entity Framework Core migrations through CI and CD in Azure DevOps

You can use the --idempotent option which will add code to the script so that only the appropriate migrations are applied to the database. This effectively makes the sql script the equivalent of running update-database in the package manager console.

With EntityFrameworkCore for example. DbContexts can be converted into databases, using the ‘dotnet ef migrations’ commands. As for how to use dotnet ef command line to handle the process.

Please kindly refer this blog-- EntityFrameworkCore, code-first migrations in Azure DevOps for more details.