Entity Framework implement Code first Migrations and prevent data loss

data-loss ef-migrations entity-framework-6 model-view-controller


I would like to know what would be the best guide to follow and things to consider if i would want to Add Migrations to a Project and note that the project:

  • is live (dev/staging/production environments)
  • Model of the live versions has changed and some fields/tables are removed/added
  • is hosted with Azure App Service (Publish settings)
  • is an MVC project with Entity Framework 6 using code first

I know the basics of adding/using migrations but that's it. I would like to know how i can implement migrations to my solution, publish the new project (changed model) without losing any data.

Is this possible and can anyone suggest me anything to look at that is well explained for this kind of setup?

EDIT I am testing this on development but i can't make it work without having my database recreated, hence losing existing data...

My configuration file:

public Configuration()
    AutomaticMigrationsEnabled = true; // tried false as well
    ContextKey = "ContractCare.Models.ApplicationDbContext";
    AutomaticMigrationDataLossAllowed = false;

Kind regards

12/20/2018 3:33:52 PM

Popular Answer

Add an empty snapshot migration to your DEV environement. This will capture the current state of that model:

Add-Migration InitialBaseline –IgnoreChanges  // Tells EF not generate Up() code of existing objects

Now all subsequent changes in DEV can be deployed to other environments either by changing the connect string and re-running or by generating a script that can be run on those servers update-database -Script.

Before that, you have to "catch up" the other environments to the state of DEV using the processes you already have in place. Then you apply the InitialBaseline migration to those environments.

Moving forward you can apply the DEV migrations to UAT, STG and eventually PROD. Since a lot of migrations tend to happen in DEV, you can roll those up into a single migration as Chris explains here.

1/16/2019 4:40:01 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow