开发者

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

开发者 https://www.devze.com 2023-03-03 01:24 出处:网络
In my Global.asax Ihave the following line: Database.SetInitializer<myDbSupport> (new DropCreateDatabaseIfModelChanges<myDbSupport>());

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 hav开发者_运维知识库e 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消