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.
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
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:
This 5 step process is what I want in one command in PMC.
@PeterOeClausen Or you can:
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.
cd src\Host
dotnet ef database drop -c PersistedGrantDbContext
dotnet ef database drop -c ConfigurationDbContext
cd ..\..
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 ..
cd host
dotnet ef database update -c PersistedGrantDbContext
dotnet ef database update -c ConfigurationDbContext
dotnet run /seed
cd ..
remove-migration -force
Most helpful comment
@PeterOeClausen Or you can: