Entityframework.docs: Show options for backing out/resetting migrations

Created on 10 Oct 2018  Â·  9Comments  Â·  Source: dotnet/EntityFramework.Docs

Due to renaming and changes to my domain classes, I have confused EF Core so that it creates migrations that no longer makes sense when executing.
Eg. I get the error:
PM> update-database
Applying migration '20181010102432_stuck'.
Failed executing DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE [Photo] DROP CONSTRAINT [FK_Photo_CubeObject_ObjectId];
System.Data.SqlClient.SqlException (0x80131904): 'FK_Photo_CubeObject_ObjectId' is not a constraint.
Could not drop constraint. See previous errors.

It somehow tries to remove a constraint it cannot find, thereby resulting in an error.

I would like to wipe all migrations and the database, and start from scratch, but and it should be as easy as to fire a command in the Package Manager Console, like: "Reset-Migrations" or "Reset-Database" (or some better naming), that would remove all migrations and wipe the database completely.

If you Google "How to reset EF Core" there are many possible solutions to this problem, but they require many manual steps.
I know that custom migration code would also be deleted, but that can be added back in manually (and it has probably changed).

I just think it's harder than it should be to reset the migrations and the database. I know many people who would like this feature.

Best, Peter.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

area-migrations punted-for-2.2

Most helpful comment

@PeterOeClausen Or you can:

  • Delete the Migrations folder
  • Run the command "drop-database" from the PMC

All 9 comments

Also, an initial migration could be added by default that made no changes to the database -this would be easier to reset to.

Also, if you (like me) were looking for a way to wipe the data and migrations, do the following:

  1. Open the 'SQL Server Object Explorer' in Visual Studio.
  2. There should be one called something like: "(localdb)\MSSQLLocalDB (SQL Server)...". Open that one, open Databases. You should be able to find the database in there. Open the your database there, then tables. Now delete all tables by right-clicking each one and clicking 'Delete'. Also 'dbo.__EFMigrationsHistory'.
  3. Delete the 'Migrations' folder in your Solution.
  4. Run 'Add-Migration yourMigrationNameHere' in the Package Manager Console.
  5. Run 'Update-Database' in the Package Manager Console.

This 5 step process is what I want in one command in PMC.

@PeterOeClausen Or you can:

  • Delete the Migrations folder
  • Run the command "drop-database" from the PMC

True, thanks for the tip :)

Maybe if you could add it to the Docs (if it's not already there somewhere), or make it easier to find :)
Eg. If you create another step called 'How to reset or clear your database and migrations':
https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/

While trying to find a solution for my problem, I found other developers searching for an easy way to do this:

So it may make sense to make it more visible in the docs?

Thanks for the help!

@PeterOeClausen This worked in EF6: Update-Database -TargetMigration:0 and this seem to be working in EF Core: Update-Database 0

@older That works to, but it attempts to roll back the migrations by running Down methods. If the migrations have been messed up (i.e. through bad hand edits) then this may not work, hence sometimes blowing away the database is more pragmatic.

@PeterOeClausen Or you can:

  • Delete the Migrations folder
  • Run the command "drop-database" from the PMC

Still would be nice if we have a command to do these steps for us. Something like reset-the-whole-world-for-me!

I like the way IdentityServer4.EntityFramework did it. They have a dropdb.bat, an updatedb.bat and a migratedb.bat. They also work with multiple db contexts.

DropDb

cd src\Host
dotnet ef database drop -c PersistedGrantDbContext
dotnet ef database drop -c ConfigurationDbContext
cd ..\..

MigrateDb

cd host
rmdir /S /Q Migrations

dotnet ef migrations add Grants -c PersistedGrantDbContext -o Migrations/IdentityServer/PersistedGrantDb
dotnet ef migrations add Config -c ConfigurationDbContext -o Migrations/IdentityServer/ConfigurationDb
dotnet ef migrations script -c PersistedGrantDbContext -o Migrations/IdentityServer/PersistedGrantDb.sql
dotnet ef migrations script -c ConfigurationDbContext -o Migrations/IdentityServer/ConfigurationDb.sql

cd ..

UpdateDB

cd host
dotnet ef database update -c PersistedGrantDbContext
dotnet ef database update -c ConfigurationDbContext
dotnet run /seed
cd ..

remove-migration -force

Was this page helpful?
0 / 5 - 0 ratings

Related issues

joakimriedel picture joakimriedel  Â·  3Comments

qidydl picture qidydl  Â·  4Comments

VirMaker picture VirMaker  Â·  4Comments

speciesunknown picture speciesunknown  Â·  3Comments

ajcvickers picture ajcvickers  Â·  4Comments