How to Unapply a Migration in ASP.NET Core with Ef Core

Is it possible to remove an EF Core Database migration after the database is updated with the same

Yes, it's possible, but only if it is the last migration or if you revert all migrations after it.

To revert all migrations including the desired, use the -Migration parameter of the same Update-Database command and pass the name/id of the migration before the desired (or special value 0 if it is the first)

Update-Database MigrationBeforeTheOneYouWantToRevert

For more info, see Applying Migrations - Command-line tools sections of the official EF Core documentation.

How to rollback more than one EF Core migration at time

The solution is to update the database back to the target migration desired:

dotnet ef database update "THE NAME OF THE TARGET MIGRATION"

Execute this Ef Core command multiple time until the desired migration:

dotnet ef migrations remove

And below my Powershell script:

param (
[Parameter(Mandatory=$true)][string]$migration,
[Parameter(Mandatory=$true)][int]$nbr_removes
)

dotnet ef database update $migration

for($i=0; $i -lt $nbr_removes; $i++) {
dotnet ef migrations remove
echo 'revert changes'
}

And we can use it like below:

.\rollback_to.ps1 2019081200218_AddScoreColumn 2

EF Core migration to remote database

So I was just using the wrong environment declaration. Run this before the update:

$env:ASPNETCORE_ENVIRONMENT = 'Production'


Related Topics



Leave a reply



Submit