1

I'm using DropCreateDatabaseIfModelChanges DB initializer class in Entity-framework-4.2. Problem is it creates a new database and old data is lost. I used seed method of this class to enter some new values. But in that way I cannot preserve my old data. Does anyone know a way to drop and create a DB when model changes, without losing existing data?

Thank you.

Lasantha Bandara
  • 809
  • 3
  • 10
  • 22
  • possible duplicate of [EntityFramework Modify Database Instead of Recreate](http://stackoverflow.com/questions/8760727/entityframework-modify-database-instead-of-recreate) – Eranga Jan 11 '12 at 06:13

1 Answers1

1

No it is not possible with built-in database initializers and writing custom initializer preserving data would be very complex task. As alternative you can use Code First Migrations instead of initializers. Migrations allows incremental development of your database.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Thank you very much for your answer Ladislav. Could you please explain what you meant by **"incremental development of database"** – Lasantha Bandara Jan 13 '12 at 04:40
  • It is what you are looking for. The way to push only schema changes to your database instead of deleting whole database and creating it from scratch (that is what initializers do). – Ladislav Mrnka Jan 13 '12 at 08:16
  • full sample if I use mdf file and I need add new table ? – Kiquenet Jun 03 '17 at 14:44
  • https://blogs.msdn.microsoft.com/b/adonet/archive/2011/11/29/code-first-migrations-beta-1-released.aspx not found – Kiquenet Jun 03 '17 at 14:45