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.
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. 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. 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. Up
method in the Reset
migration, so that new databases can be scripted to this point. If you want to maintain your migration history
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)
);
update-database
command: it will refill __MigrationHistory
without attempting to do anything else Point your scripts to another empty database. Run all your migrations. Copy table and data from __MigrationHistory
into your production database.
上一篇: 初始代码迁移不起作用
下一篇: 如何恢复数据库EF迁移工具