[英]Using EF4 Code First: How can I change the Model without losing data
In my Global.asax I have the following line:在我的Global.asax我有以下行:
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.如果我没有这个,那么当我更改 model 时, sdf数据库将没有正确的结构。 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?是否可以编写数据库更改脚本,并在部署具有更改结构的 model 之前运行此更新?
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.其他人可以简单地忘记它的存在,重新部署 single.dll,你的数据库就消失了。
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.数据库升级是应作为升级脚本的一部分单独执行的操作,安装 package 或在应用程序维护期间手动升级,而不是在首次请求新版本期间。 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.一旦你有了一个脚本,你就可以在完成后简单地执行它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.