Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34152

How to remove all migrations without unapplying them

I have a .NET Core Project with lots of migrations in it. I also have the database (given to me and not generated with migrations on my pc). now

  1. When I try to add a migration I get an error that there are pending migrations and I first need to update database and I you can guess running update-database command gives me:

object ... already exists error

  1. If I remove database update-database command will generate the whole database however there are lots of data in the database that creating data with migrations would wipe them out.

I thought of generating data script from database, then creating database with migrations and then running the script, but the data script is very large and running the script have lots of other issues.

I just need to remove the old migrations but not unapplying them (as it would also remove my tables from database).

And also note that there are no _MigrationHistory Table in the database

Upvotes: 1

Views: 267

Answers (1)

Murat Yıldız
Murat Yıldız

Reputation: 12022

If there is no __MigrationHistory table in the database and you want to omit the migrations, you can also comment the Up and Down fields in the migration files and apply migration. It does not affect your database and only add migration record to __MigrationHistory table.

Note: This problem might occur sometimes and using the approach above most probably fix the problem without affecting data. There are of course some other workarounds that can be applied for such a kind of problem.

Upvotes: 2

Related Questions