gezanoletti
gezanoletti

Reputation: 147

How to change models without losing data in production in Entity Framework 6 Code-First?

I'm using Entity Framework 6 Code-First and my application is in production. I have to perform some changes in my models without lose any data.

I have to perform changes like these:

I need something like the Seed Database, but I need to load the data before change the model to later insert it again... I need to move the data. When I change the relationship I don't lose the data but the relation do.

How can I do this process?

Thanks so much for any help.

Upvotes: 0

Views: 2625

Answers (1)

Cam Bruce
Cam Bruce

Reputation: 5689

I would suggest you follow these steps:

  1. create temporary tables that represent the data & relationships of the affected tables. Ensure you do this outside of EF, using CREATE TABLE sql
  2. BEFORE running Update-Database, run a script to move data from the tables in question to your temp tables
  3. Run Update-Database
  4. Run script to insert data and relationships back into your new tables
  5. Drop temp tables

Upvotes: 2

Related Questions