skip to Main Content

I’m creating a new code-first Web application but using the code from another application as a starting point. But I’d prefer to delete all the unneeded migrations from the new project.

Currently, my migrations look like this:

Visual Studio Data Migrations

I’m pretty sure that I need to keep ApplicationDbContextModelSnapshot.cs and ApplicationDbContext.cs.

So here are my question is: Can I safely delete all the other migration files?

Note: There is no change I will want to revert migrations in the new application, so that is not a concern.

2

Answers


  1. Why not just delete them and add an empty migration to check? If it breaks then you have your answer. Then just restore from version control.

    But what Kirk Woll said is correct. Keep your ApplicationDbContext and add an intial migration.

    Login or Signup to reply.
  2. To summarize your question, you’d like to have a clean slate and not have all the migrations you’ve accrued over time polluting your migrations folder. I’ve often needed to do this myself when starting a new project and iterating a lot before being satisfied with my final schema.

    The good news is that Entity Framework supports this natively out of the box, with just one caveat: if you ever hand-roll your own migrations (by calling Add-Migration and then editing the resultant migration file), perhaps to add a custom index for perf reasons, you’ll have to make sure to recover those in this process.

    All you have to do is delete your entire migrations folder (including the snapshot) and start over as though you were creating the project from scratch. Typically when you create your first migration, you’ve already defined a few entities (that’s why you want to create the migration, after all); you’re just taking that to the next level and want your entire database in that migration.

    Once you’ve deleted your migrations folder, EF no longer thinks your project has migrations at all — like I said, you’re starting from scratch. So all you have to do is call Add-Migration [MigrationName] (i.e. Add-Migration InitialMigration) and it will scaffold the migration with everything needed to create your whole database.

    Note: In my comments, I suggested you would need to call Enable-Migrations first, but that is evidently no longer necessary so you can just jump right into calling Add-Migration.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search