简体   繁体   English

使用 EF4 代码优先:如何在不丢失数据的情况下更改 Model

[英]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.

相关问题 EF4 Database First Model 基于正确规范化表的设计 - EF4 Database First Model Design based on properly normalized table 在EF4中,如果没有第一个获取实体就无法更新实体 - In EF4 unable to Update Entity Without First Getting Entity EF4 Code First和SQLCE 4.0-生成数据库时发生异常 - EF4 Code First & SQLCE 4.0 - Exception when generating DB ASP.Net EntityDataSource 与 EF4 代码优先 - ASP.Net EntityDataSource vs. EF4 Code First 如何使用Entity Framework Code First更新数据库结构而不丢失所有数据? - How to update structure of database with Entity Framework Code First without losing all data? 我可以让EnableUpdate和EnableDelete在EF4 LinqDataSource上工作吗? - Can I get EnableUpdate and EnableDelete to work on an EF4 LinqDataSource? 更改asp.net mvc模型的EF Code First模型后,如何在Azure中正确更新SQL数据库? - How to properly update SQL database in Azure after EF Code First model of asp.net mvc model change? 如何在不丢失用户保存密码的情况下更改登录页面? - How can I change my login page without losing users' saved passwords? EF4-在没有保存更改的情况下将对象添加到对象上下文 - EF4 - Add object to objectcontext without savechanges 使用EF多对多代码进行首次开发时,如何将数据绑定到转发器? - The code first development using the EF many-to-many how to bind data to repeater?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM