首先从实体框架模型中的模型生成数据库时如何进行迁移
我有一个现有模型,最初从该模型生成数据库,并用一些数据填充现有表。现在我已向模型添加了一个新表。有没有办法从新模型更新数据库而不丢失现有表中的所有数据?谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
EF 的默认数据库生成工作流程会创建一个完整的脚本,每次您选择从模型生成数据库...时,该脚本都会重新创建数据库,因此如果您在数据库中执行它,您将丢失所有数据。但是,如果您只是创建一个新实体并且没有更改现有实体,那么您仍然可以从模型生成数据库,但然后采用该脚本并仅运行为新实体创建新表的部分。
另一种方法是安装Entity Microsoft 的 Designer Database Generation Power Pack 并未出现在 EF4.0 版本中。安装后,当您从模型生成数据库时,将您的数据库生成工作流程更改为生成迁移TSQL:
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: