azure-devopsentity-framework-core

Run "dotnet ef migrations script" from the last migration


In Azure DevOps I have a build task with the following command:

dotnet ef migrations script --output path/migrations-script.sql --idempotent

In my release pipeline I run the generated script.

The issue I've got is that the above command runs from the first migration. I know I can specify a from argument for this command, but you have to specify the name of migration. Is there any way to generate the script from the last migration without specifying its name?


Solution

  • Is there any way to generate the script from the last migration without specifying its name?

    As I know, it's necessary for us to specify the name when we try to generate script from last migration, unless we have only one migration.

    See the details about dotnet ef migrations script:

    Arguments:

    From: The starting migration. Migrations may be identified by name or by ID. The number 0 is a special case that means before the first migration. Defaults to 0.

    To: The ending migration. Defaults to the last migration.

    So if we have more than one migrations, we have to specify From with the start migration we want. This behavior is by design. Check this post, to generate script from last migration we should use format like:

    dotnet ef migrations script -from (MigrationYouWant-1) -to MigrationYouWant

    So I'm afraid there's no other way to do this in Devops pipeline or local machine, we have to specify the command with name. Hope my answer can help to clarify something for you:)