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

我只是一个虾纸丫 提交于 2019-12-18 12:29:49

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/5870037/using-ef4-code-first-how-can-i-change-the-model-without-losing-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!