Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using EF4 Code First: How can I change the Model without losing data

In my Global.asax I have the following line:

Database.SetInitializer<myDbSupport>
    (new DropCreateDatabaseIfModelChanges<myDbSupport>());

If I don't have this, then when i change the model, the sdf database will not have the correct structure. This is ok in a dev environment, but when I move to production and want a DB structure update, i of course, can't afford to drop the table, and lose the data.

Is it possible to script DB changes, and run this update before deploying the model with the changed structure?

like image 861
JAG Avatar asked May 03 '11 13:05

JAG


1 Answers

Initializer is for development. I consider any automatic process changing your production database directly from application as evil. Somebody else can simply forget the existence of it, redeploy single .dll and your database is gone.

Database upgrade is operation which should be executed separately as part of upgrade script, installation package or manual upgrade during application maintenance and not during first request to the new version. I described migration yesterday.

What you are looking for is custom intializer which would execute external script created in my linked answer. That can be partially working if you include a lot of additional checks which will avoid running script twice. But why? Once you have a script you can simply execute it once an you are done.

like image 86
Ladislav Mrnka Avatar answered Nov 15 '22 20:11

Ladislav Mrnka