如何首先使用 EF 4 代码重构代码/数据库架构 - 数据迁移

发布于 2024-09-16 21:15:37 字数 469 浏览 4 评论 0原文

使用 codefirst EF4 进行数据库重构的最佳实践是什么?

我很想听听当 RecreateDatabaseIfModelChanges 选项不可行时人们如何更改类和数据库。需要进行数据迁移。

目前,Microsoft 有一个首先使用模型来执行此操作的解决方案:

http://blogs.msdn.com/b/adonet/archive/2010/02/08/entity-designer-database- Generation-power-pack.aspx ?PageIndex=2#comments

有人有代码优先的好策略吗?

What are the best practices for database refactoring with codefirst EF4?

I am interested to hear how people change the classes and the database when the RecreateDatabaseIfModelChanges option is not feasible. Migration of data will need to occur.

Currently Microsoft has a solution for doing this with model first:

http://blogs.msdn.com/b/adonet/archive/2010/02/08/entity-designer-database-generation-power-pack.aspx?PageIndex=2#comments

Does anyone have a good strategy for code first?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

动次打次papapa 2024-09-23 21:15:37

EF 团队一直致力于为 EF 开发迁移功能,以解决此问题。

http:// /blogs.msdn.com/b/efdesign/archive/2010/10/22/code-first-database-evolution-aka-migrations.aspx

Scott Gu 在他最近的欧洲之旅中表示,他们应该发布很快就有这个功能了。我屏住呼吸。

激动人心的更新:

现已作为 CTP 发布:
http ://blogs.msdn.com/b/adonet/archive/2011/07/27/code-first-migrations-august-2011-ctp-released.aspx

The EF team have been working on a migrations feature for EF that should solve this problem.

http://blogs.msdn.com/b/efdesign/archive/2010/10/22/code-first-database-evolution-aka-migrations.aspx

Scott Gu said on his recent tour around Europe that they should be releasing this feature soon. I'm holding my breath.

EXCITING UPDATE:

This has now been released as a CTP:
http://blogs.msdn.com/b/adonet/archive/2011/07/27/code-first-migrations-august-2011-ctp-released.aspx

帅的被狗咬 2024-09-23 21:15:37

我正在研究数据库上下文初始化程序,如果模型和数据库架构不同步,它将通知网站管理员,并显示不同之处。这对于喜欢完全控制代码优先模型和数据库模式的开发人员来说非常有用。看看:

https://github.com/rialib/efextensions

I am working on database context initializer which will notify webmaster if model and db schema are out of sync and will show what differs. This can by useful for developers who prefer to have complete control both over code-first model and database schema. Check it out:

https://github.com/rialib/efextensions

寂寞笑我太脆弱 2024-09-23 21:15:37

在我的 CodeFirst 应用程序中,本地构建有一个 app.config 标志,表示不在生产中。当我不在生产中时,它会完全破坏并重新创建数据库。由于我的生产数据库用户没有删除数据库的权限,即使我的 web.config 转换以某种方式丢失(因此 EF 尝试重新创建数据库),我的生产数据库也不会被删除,而是会引发异常。

我的工作流程是这样的:

  1. 检查具有最新更改的代码的生产分支
  2. 快速冒烟/回归测试(这应该在将代码签入生产分支之前完成,但以防万一)
  3. 下载我的生产数据库的最新备份并安装它在我的本地 SQLEXPRESS 服务器上
  4. 运行 Open DBDiff 在我的本地代码创建的数据库之间(即使它是生产代码,因为它是本地的(它会根据生产备份重新创建数据库)。
  5. 检查生成的脚本并尝试针对生产备份运行它们
  6. 假设没有发生错误,则覆盖使用生产备份生成的代码的数据库,并对生产数据进行测试以确保所有数据仍然完好无损;
  7. 在实际生产数据库上运行脚本。

步骤#2 根据最新的数据模型自动创建一个新的、干净的数据库,因此我始终知道我拥有一个最新的数据库,该数据库不包含可能尚未准备好生产的开发工作的工件。

In my CodeFirst application, local builds have a app.config flag that denotes not being in production. When I'm not in production it completely nukes and recreates the database. Since my production database user does NOT have permissions to drop the database even if my web.config transform is missed somehow (thus EF tries to recreate the database) my production database will not be deleted, and instead an exception will be thrown.

My workflow goes like this:

  1. Check out production branch of code with latest changes
  2. Quickly smoke/regression test (this should already be done prior to checking code into the production branch, but just in case)
  3. Download the latest backup of my production database and install it on my local SQLEXPRESS server
  4. Run Open DBDiff between the database my local code created (even though it's production code, since it's local it recreates the database) against the production backup.
  5. Review scripts generated and attempt to run them against the production backup
  6. Assuming no errors occurred, overwrite the database that the code generated with the production backup and do testing against the production data to make sure all data is intact still;
  7. Run scripts on the real production database.

Step #2 automatically creates a new, clean database based on the latest data model so I always know I have an up to date database that doesn't have artifacts from development efforts that may not be production ready yet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文