1

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

4
  • You could change the connection string to something different so .net core will think there is no db and then remove the migrations. when you are done removing all migrations, you can change the connection string back to your existing db. But I'm not sure, what your goal is. Maybe you should consider changing from code-first to db-first? Commented May 25, 2020 at 18:59
  • Visual studio is getting smarter, this trick didn't work Commented May 25, 2020 at 19:15
  • The missing _MigrationHistory is a bummer. You could recreate that table manually (it's not that complicated anyway) but that would require you to know which was the last migration applied to that database. I'd try to compare the model snapshot with the database and try to see if the database matches the model snapshot. If everything matches, then just create the the dbo.__EFMigrationsHistory table and add records manually. Note that I assume you use the latest EF Core (v3.1.x) Commented May 25, 2020 at 20:30
  • apparently they had renamed the __MigrationHistory Table, I just renamed it back, and added some missing migrations and managed to get it to work. Commented May 25, 2020 at 20:37

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

çok teşekkür ederim Murat Bey.
@AshkanMobayenKhiabani You are welcome Ashkan Bey, regards ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.