Alex Kudryashev
Alex Kudryashev

Reputation: 9480

Combine migrations in MVC 5 Code First Project

Very quick question.

I work on a MVC 5 Code First project and I do a lot of small changes in the DB models. Sometimes the changes overwrite previous ones. Is it safe to update-database -targermigration InitialCreate and then remove all migrations (except initial) and add-migration cumulative ? I expect expert opinions like Yes or No. Thank you in advance.

Upvotes: 0

Views: 632

Answers (1)

Maria Ines Parnisari
Maria Ines Parnisari

Reputation: 17506

It'd be interesting to know other people's practices on this, but I'll add what I would do. (Disclaimer: I haven't done this myself in a long time)

A bit of context first. Code migrations are snippets of code telling the database how to update itself after you make a change in your model classes. Since you're doing development and you haven't pushed code to production, there's no point in doing migrations because there is no production database that needs taking care of. For now, if needed, you could simply delete your local DB and rebuild it from scratch every time you make a change. But this is time consuming.

But there is a better way: automatic migrations. Every time you make a change and call Update-Database, EF will (behind the scenes) create a migration and update the database for you.

  1. Run Enable-Migrations –EnableAutomaticMigrations.
  2. Every time you make a model change, run Update-Database.

Note that in some changes will still require that you add a migration manually, as specified here.

Now, this is obviously not suited for a production environment, for a variety of reasons.

So when you're done building the MVP version of your app, I would just delete the local DB, and override the initial migration as shown here.

  1. Delete InitialCreate.cs (or the entire Migrations folder for that matter...)
  2. Run Add-Migration InitialCreate
  3. Delete the local database.
  4. Run Update-Database -Force.

Upvotes: 1

Related Questions