Stephane
Stephane

Reputation: 3402

EF Core (3.1) tools: is there a way to downgrade a database to just the previous migration?

We are writing an EF Core 3.1 application in C#. Currently, the data model is in constant change so I wrote a couple of scripts to automatise the downgrade DB/remove migration/add migration/update DB cycle.

This serves me well for the first migration but I'm reaching the point where I'd like to keep the base data between cycles: add a new migration and only revert and re-create the last one.

for removing and addning migration as well ad upgrading the database, there is no problem. But I don't have any command to revert the database just to the previous migration: I can use dotnet ef database update to upgrade to the latest, dotnet ef database update 0 to remove all migrations but that's it.

Any suggestion?

Edit: I might not have made my real issue clear: I don't know the name of the last migration in my script so I can't reference it in dotnet ef database update <last migration name>

Upvotes: 4

Views: 2910

Answers (3)

Kamran
Kamran

Reputation: 1380

How about getting your last migration name using the following command. As far as I remember there is a filter switch which I don't remember.

dotnet ef migrations list

Update:

dotnet ef migrations list | select -last 2 | foreach {*do something here*$_}

Now, you can put your update or migration command there.

Upvotes: 3

IzyPro
IzyPro

Reputation: 83

For anyone else with the same problem making use of visual studio. Navigate to the package manager console. View > Other windows > Package manager console

Update-Database MigrationName This reverts the database to the migration specified.

Remove-Migration Removes the last migration.

Upvotes: 1

milo
milo

Reputation: 520

dotnet ef database update <previous-migration-name> and then dotnet ef migrations remove to remove last migration

e.g. If you have migration1 -> migration2 -> migration3 then in order to revert your database to previous migration you would run dotnet ef database update migration2 and then dotnet ef migrations remove

Upvotes: 4

Related Questions