How to restore database EF migration tools

I got a database, and accidentally I deleted the __MigrationHistory table. Now the program throws error, and I can't loss all the datas. Is there anyway to restore the table that I deleted?? Will I lost all my database?

I added the __MigrationHistory manually, and now this is the error:

Additional information: Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration


You can "reset" the state of your migrations in your project. You will essentially be creating a migration state where Entity Framework believes that the current state of your database is the "first" migration. Note, however, that this will limit your ability to roll back to previous versions of the app with an earlier migration state.

  • Delete the existing migrations from the migrations folder in your project.
  • Delete the __MigrationHistory table in the database (Already Done).
  • Run the following command in the Package Manager Console: add-migration Reset . because the migrations folder does not contain any previous migrations, the Reset migration will be a full script of the models in their current state. Important Verify that this Up method matches exactly the current database table state.
  • In the Reset migration, comment out everything in the Up method. We don't want to run the Up method, because the Database should already match this.
  • Run the update-database command. This will create a new __MigrationHistory table and create a new line in the table, indicating that the database is matching this Reset migration step. It won't make any changes to the database, however, since the Up method is empty.
  • (optional) Remove the comments around the Up method in the Reset migration, so that new databases can be scripted to this point.

  • If you want to maintain your migration history

  • If you both deleted content and structure of _MigrationHistory, create an empty _MigrationHistory with
  • CREATE TABLE [dbo].[__MigrationHistory] (
        [MigrationId]    NVARCHAR (150)  NOT NULL,
        [ContextKey]     NVARCHAR (300)  NOT NULL,
        [Model]          VARBINARY (MAX) NOT NULL,
        [ProductVersion] NVARCHAR (32)   NOT NULL,
        CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY CLUSTERED ([MigrationId] ASC, [ContextKey] ASC)
    );
    
  • Comment the contents of the Up() methods of all your migration code files
  • Run the update-database command: it will refill __MigrationHistory without attempting to do anything else
  • Uncomment what you commented in your migration code files in order to create the db model when you will deploy on a new database

  • Point your scripts to another empty database. Run all your migrations. Copy table and data from __MigrationHistory into your production database.

    链接地址: http://www.djcxy.com/p/90254.html

    上一篇: 初始代码迁移不起作用

    下一篇: 如何恢复数据库EF迁移工具