在实体框架模型中从模型生成数据库时,如何进行迁移 [英] How to do Migration when Generate database from model in Entity Framework Model First

查看:103
本文介绍了在实体框架模型中从模型生成数据库时,如何进行迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的模型,最初从这个模型生成数据库,并用一些数据填充了现有的表。现在我已经添加了一个新的表到模型。有没有办法从新模型更新数据库,而不会丢失现有表中的所有数据?感谢。

解决方案

EF的默认数据库生成工作流创建一个完整脚本,每次选择生成数据库时将重新创建数据库从模型... ,所以如果你在你的数据库中执行它,你将丢失所有的数据。但是,如果您只创建一个新的实体并且没有更改现有的实体,那么您仍然可以从模型生成数据库,但是仍然使用该脚本,并且仅运行为新实体创建新表的部分。



另一种方法是安装 实体设计器数据库生成Power Pack ,并没有使其成为EF4.0版本。安装后,当您从模型生成数据库时,将您的数据库生成工作流程更改为生成迁移TSQL




I have an existing model and initially generated the database from this model and had populated the existing tables with some data. Now I've added a new table to the model. Is there a way to update the database from the new model without losing all the data in the existing tables? Thanks.

解决方案

EF's default database generation workflow creates a full script that will recreate your database every time you select Generate Database from Model... so if you execute it in your DB you will lose all your data. However, if you just create a new Entity and did not change the existing ones, then you can still generate database from your Model but then take that script and only run the part that creates the new table for your new entity.

Another way would be to install Entity Designer Database Generation Power Pack from Microsoft which didn't make it to be in EF4.0 release. After you install it, change your database generation workflow to Generate Migration TSQL when you generate Database from your Model:

这篇关于在实体框架模型中从模型生成数据库时,如何进行迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆