admin管理员组

文章数量:1202802

I'm using ASP.NET Core 6 MVC, EF with Microsoft SQL Server. I have many migrations files for starting new feature of project.

The latest migration file before start is MigrationOld.

I have these tables:

invoiceTable
customerTable

And the feature development migration files are:

migrationNewOne
migrationNewTwo
migrationNewThree

The latest migration (migrationNewThree) adds these tables:

vendorTable
vendorAddressTable

I have applied updated database to new migrationNewThree, but I have accidentally lost migration file of migrationNewThree and I try to revert database by applying migrationOld.

In the console, I get no error and it is applied with success. But it does not affect the database, all new feature tables still exist in the database.

I would like to find the solution by reverting the database to migrationOld and clean all those table from new feature and re-apply feature table again.

How can I do that?

I'm using ASP.NET Core 6 MVC, EF with Microsoft SQL Server. I have many migrations files for starting new feature of project.

The latest migration file before start is MigrationOld.

I have these tables:

invoiceTable
customerTable

And the feature development migration files are:

migrationNewOne
migrationNewTwo
migrationNewThree

The latest migration (migrationNewThree) adds these tables:

vendorTable
vendorAddressTable

I have applied updated database to new migrationNewThree, but I have accidentally lost migration file of migrationNewThree and I try to revert database by applying migrationOld.

In the console, I get no error and it is applied with success. But it does not affect the database, all new feature tables still exist in the database.

I would like to find the solution by reverting the database to migrationOld and clean all those table from new feature and re-apply feature table again.

How can I do that?

Share Improve this question edited Jan 21 at 5:39 DarkBee 15.6k8 gold badges70 silver badges114 bronze badges asked Jan 21 at 1:28 KuthKuth 155 bronze badges 2
  • (1) If you follow the practice of checking in all migrations to a source code repository before applying, you can look for old versions there. (2) If you work in an enterprise environment where your company has set up scheduled remote backups for your local working folders, you can look there. (3) If you have database backups, you can look there for a pre-migration copy from which you can perform a compare. (Note that in addition to schema changes, your migrations may have also added, modified, or removed data.) (4) If none of the above apply, it's time to update your devops policies. – T N Commented Jan 21 at 2:46
  • So the best way is restore database backup from last date before migration new feature right? – Kuth Commented Jan 21 at 3:47
Add a comment  | 

1 Answer 1

Reset to default 0

Firstly, if you have all the migration file which include the migrationOld . If you lose one migration file, EF Core cannot properly manage the schema for those changes because it has no reference to what was added or removed.

Since migrationNewThree is missing, EF Core cannot generate the SQL required to revert the changes. You will need to manually drop the new feature tables (vendorTable and vendorAddressTable) from your database.

DROP TABLE vendorAddressTable;
DROP TABLE vendorTable;

Then you could run the command to update the database from none to the MigrationOld

dotnet ef database update MigrationOld

本文标签: aspnet core mvcHow to revert to Previous Migration while Current Database is appliedStack Overflow