You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Show options for backing out/resetting migrations #1048
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.
ID: 6e28b0a8-b7e4-4c07-9e60-0df403bbee28
Version Independent ID: 12300b31-6f7b-b75c-970d-693365e77013
Reacted by Tohid, Anshul Vishwakarma, Steven Rasmussen, Makla, Klassy, Necip Sunmaz, Hoon Chung, Igor Moskvitin, Matthew Van der Bijl, Almis90 and 2 more
Also, if you (like me) were looking for a way to wipe the data and migrations, do the following:
Open the 'SQL Server Object Explorer' in Visual Studio.
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'.
Delete the 'Migrations' folder in your Solution.
Run 'Add-Migration yourMigrationNameHere' in the Package Manager Console.
Run 'Update-Database' in the Package Manager Console.
This 5 step process is what I want in one command in PMC.
@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.
Reacted by Tohid, Hoon Chung and Peter Øvergård Clausen
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 ..
changed the title [-]There should be a 'Reset' command that would delete all migrations, the migration history, and clear the database.[/-][+]Show options for backing out/resetting migrations[/+]on Aug 29, 2019
Activity
PeterOeClausen commentedon Oct 10, 2018
Also, an initial migration could be added by default that made no changes to the database -this would be easier to reset to.
PeterOeClausen commentedon Oct 10, 2018
Also, if you (like me) were looking for a way to wipe the data and migrations, do the following:
This 5 step process is what I want in one command in PMC.
ajcvickers commentedon Oct 10, 2018
@PeterOeClausen Or you can:
PeterOeClausen commentedon Oct 10, 2018
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:
https://weblog.west-wind.com/posts/2016/Jan/13/Resetting-Entity-Framework-Migrations-to-a-clean-Slate
https://stackoverflow.com/questions/16035333/how-to-delete-and-recreate-from-scratch-an-existing-ef-code-first-database
https://stackoverflow.com/questions/22680446/entity-framework-rollback-and-remove-bad-migration
So it may make sense to make it more visible in the docs?
Thanks for the help!
older commentedon Oct 12, 2018
@PeterOeClausen This worked in EF6:
Update-Database -TargetMigration:0and this seem to be working in EF Core:Update-Database 0ajcvickers commentedon Oct 12, 2018
@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.
tohidazizi commentedon Oct 19, 2018
Still would be nice if we have a command to do these steps for us. Something like
reset-the-whole-world-for-me!VictorioBerra commentedon Oct 22, 2018
I like the way IdentityServer4.EntityFramework did it. They have a
dropdb.bat, anupdatedb.batand amigratedb.bat. They also work with multiple db contexts.DropDb
MigrateDb
UpdateDB
[-]There should be a 'Reset' command that would delete all migrations, the migration history, and clear the database.[/-][+]Show options for backing out/resetting migrations[/+]Improvements to migration docs (#2430)