Entity Framework 4.1 自定义数据库初始化器策略
我想实现自定义数据库初始化策略,以便我可以:
- 如果不存在,则生成数据库
- 如果模型更改 仅创建新表
- 如果模型更改仅创建新字段 而不删除表并丢失数据。
提前致谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想实现自定义数据库初始化策略,以便我可以:
提前致谢
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
您需要实现 IDatabaseInitializer 接口。
例如
,然后在应用程序启动时设置初始化程序
这是一个 示例
您将必须手动执行命令来更改数据库。
您可以使用 SQL Compare 等工具来更改脚本。
You need to implement IDatabaseInitializer interface.
Eg
And then set your initializer at your application startup
Here's an example
You will have to manually execute commands to alter the database.
You can use a tool like SQL Compare to script changes.
这尚不存在是有原因的。它非常复杂,而且
IDatabaseInitializer
接口还没有做好准备(没有办法使这种初始化与数据库无关)。您的问题“太宽泛”,无法得到令您满意的答复。根据您对 @Eranga 正确答案的反应,您只是希望有人会一步一步告诉您如何做到这一点,但我们不会 - 这意味着我们将为您编写初始化程序。做你想做的事需要什么?
即使 ADO.NET 团队没有实现这一点,他们将来也不会实现它。相反,他们正在致力于所谓的迁移。
编辑:
ObjectContext
确实可以返回用于数据库创建的脚本 - 这正是默认初始值设定项所使用的。但它对你有什么帮助呢?您要解析该脚本以查看发生了什么变化吗?您是否要在另一个连接中执行该脚本以使用与当前数据库相同的代码来查看其结构?是的,您可以创建一个新数据库,将数据从旧数据库移动到新数据库,删除旧数据库并重命名新数据库,但这是您能想象到的最愚蠢的解决方案,没有数据库管理员会允许这样做。即使这个解决方案仍然需要分析更改以创建正确的数据传输脚本。
自动升级是一个错误的方式。您应该始终在某些工具的帮助下手动准备升级脚本,对其进行测试,然后手动执行它或作为某些安装脚本/包的一部分。在进行任何更改之前,您还必须备份数据库。
There is a reason why this doesn't exist yet. It is very complex and moreover
IDatabaseInitializer
interface is not very prepared for such that (there is no way to make such initialization database agnostic). Your question is "too broad" to be answered to your satisfaction. With your reaction to @Eranga's correct answer you simply expect that somebody will tell you step by step how to do that but we will not - that would mean we will write the initializer for you.What you need to do what you want?
sys
views and you must know how to query them to get data about current database structure.Even ADO.NET team doesn't implemented this and they will not implement it in the future. Instead they are working on something called migrations.
Edit:
That is true that
ObjectContext
can return you script for database creation - that is exactly what default initializers are using. But how it could help you? Are you going to parse that script to see what changed? Are you going to execute that script in another connection to use the same code as for current database to see its structure?Yes you can create a new database, move data from the old database to a new one, delete the old one and rename a new one but that is the most stupid solution you can ever imagine and no database administrator will ever allow that. Even this solution still requires analysis of changes to create correct data transfer scripts.
Automatic upgrade is a wrong way. You should always prepare upgrade script manually with help of some tools, test it and after that execute it manually or as part of some installation script / package. You must also backup your database before you are going to do any changes.
实现这一目标的最佳方法可能是迁移:
http://nuget.org/List/Packages/ EntityFramework.SqlMigrations
好博客文章此处 和 这里。
The best way to achieve this is probably with migrations:
http://nuget.org/List/Packages/EntityFramework.SqlMigrations
Good blog posts here and here.