Skip to content

Show options for backing out/resetting migrations #1048

Closed

Description

@PeterOeClausen

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.

Activity

  1. PeterOeClausen commented on Oct 10, 2018

    @PeterOeClausen
    Author

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

  2. PeterOeClausen commented on Oct 10, 2018

    @PeterOeClausen
    Author

    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.

  3. ajcvickers commented on Oct 10, 2018

    @ajcvickers
    Contributor

    @PeterOeClausen Or you can:

    • Delete the Migrations folder
    • Run the command "drop-database" from the PMC
  4. PeterOeClausen commented on Oct 10, 2018

    @PeterOeClausen
    Author
  5. older commented on Oct 12, 2018

    @older

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

  6. ajcvickers commented on Oct 12, 2018

    @ajcvickers
    Contributor

    @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.

  7. tohidazizi commented on Oct 19, 2018

    @tohidazizi

    @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!

  8. VictorioBerra commented on Oct 22, 2018

    @VictorioBerra

    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 ..
  9. added this to the 2.2.0 milestone on Nov 10, 2018
  10. added
    help wanted
    on Feb 21, 2019
  11. modified the milestones: 2.2.0, 3.0.0 on Feb 21, 2019
  12. 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
  13. removed
    help wanted
    on Aug 29, 2019
  14. modified the milestones: 3.0.0, Backlog on Aug 29, 2019
  15. added a commit that references this issue on Jun 29, 2020
    2c06834
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions