How to recover from deleting Migration History in ASP.NET core using Entity Framework Core

David Lane-Joynt 41 Reputation points
2023-05-02T19:41:50.1366667+00:00

I have accidently deleted the EFMigrationsHistory table in my local setup by importing the entire database from the production site (backing it up and then restore it in local database). I cannot now update the database from a new migration.

I have tried manually updating the EFMigrationHistory table with the Migration identities from my Migrations folder in the Visual Studio solution explorer, but this does not work. The update-database EF command gives an error message that it is trying to create a table which already exists.

Do I have any alternative to deleting the local database and recreating it from the migrations in which case I will have to import the data from my production database. Not a very happy thought.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
742 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,860 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,560 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,491 Reputation points Microsoft Vendor
    2023-05-03T12:12:20.6966667+00:00

    @David Lane-Joynt, Welcome to Microsoft Q&A, based on my test, I reproduced your problem.

    Please delete all existing migrations from the Migrations Folder.

    After deleting these migrations, I find that I could use Add-Migration and Update-DataBase command to update database successfully without the problem.

    Hope my advice could help you.

    Best Regards,

    Jack

    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.  

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.