首先从EF 5.0代码迁移到数据库:清理哪些文件? [英] Migrating from EF 5.0 code first to database first: What files to cleanup?

查看:78
本文介绍了首先从EF 5.0代码迁移到数据库:清理哪些文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将基于实体框架5.0的项目从基于代码优先迁移到数据库首先。我们将使用标准的VS2012向导从数据库生成edmx模型,但是我们还有其他步骤呢?我想我必须删除所有的类,我已经定义了代码优先模型,甚至是迁移文件夹 - 除了这两个之外的任何其他清理操作?

When migrating an Entity Framework 5.0 based project from code-first to database-first. We'll be using the standard VS2012 wizard to generate the edmx models off the database but do we have any additional steps beyond that? I presume I'd have to delete all classes where I've defined the code-first models and even the migrations folder - any other cleanup operations beyond those two?


回报。

: Reporting back.

所以实际的经验是我原来的期望与Ladislav所提到的(就像他说的具体条件是依赖代码)。对我来说,整个操作大约需要15-20分钟,主要涉及到

So the actual experience was between my original expectations and what Ladislav mentioned (like he said, exact conditions are code dependant). For me the entire operation took about 15-20 minutes mostly involving


  • 分支创建(安全,万一爆炸!)

  • 删除代码优先级和源代码(我将其移动到VS2012之外进行差异引用)

  • 从数据库创建EDMX模型(传递相同的命名空间等到向导减少差异程度)

  • 快速检查代码优先类和自动生成的db-first类。这大概是1:1,因为我们在代码第一个模型中使用了明智的名字。

  • 逐个编译和修复每个错误

  • 注意到许多错误是由于自动多元化EF 5与我自己的不同领域的多元化。通过快速区分大小写的搜索修复40多个错误 - >替换所有

  • 编译后重新运行所有测试

  • 将功能分支返回! li>
  • Branch creation (safety, in case stuff blows up!)
  • Removal of Code-first classes and source (I moved them outside VS2012 for a diff reference)
  • Creation of the EDMX model from the database (passing same namespace etc to the wizard to reduce the extent of differences)
  • Quick inspection of the code-first classes and the auto-generated db-first classes. This was mostly 1:1 since we used sensible names in the code first model to begin with.
  • Compiling and fixing each error one by one
  • Noticed many errors were due to automatic pluralization of EF 5 vs my own pluralization of different fields. Fixed 40+ errors via a quick case sensitive search-> replace all
  • Rerun all the tests after compiling
  • Merge feature branch back in!

谢谢

推荐答案

如果您的实体或上下文类包含任何其他逻辑,则非常复杂。您必须:

This may be quite complex if your entity or context classes contains any additional logic. You must:


  • 将EDMX模型添加到您的项目中,让它生成实体类和上下文类

  • 删除迁移

  • 如果您的原始上下文类包含任何其他代码,则必须将其转换为与新生成的上下文类相同的名称,命名空间和程序集的部分类,并附加代码。 / li>
  • 如果您的任何实体类包含任何其他代码,则必须遵循与上下文相同的过程。

  • 如果实际调用了附加代码现在自动生成代码的内部,您必须进行其他更改,包括更改代码生成模板和使用部分方法。

  • 必须从引导程序中删除任何数据库初始化或迁移执行

  • 您必须使用引用元数据文件的新连接字符串

  • Add EDMX model to your project and let it generate entity classes and context class
  • Remove migrations
  • If your original context class contains any additional code you must convert it to partial class with the same name, namespace and assembly as newly generated context class with only that additional code.
  • If any of your entity classes contained any additional code you must follow the same procedure as with the context.
  • If the additional code was actually called from inside of the code which is now auto generated you must make some other changes which may include changing code generation template and using partial methods.
  • You must remove any database initialization or migration execution from your bootstrapper
  • You must use the new connection string referencing metadata files

另一个更复杂的选项不是使用汽车发电专长ure并使用您的旧上下文和实体类,但在这种情况下,您将不得不手动将它们保存在EDMX中。

Another more complex option is not using auto generation feature and use your old context and entity classes but in such case you will have to keep them in sync with your EDMX manually.

这篇关于首先从EF 5.0代码迁移到数据库:清理哪些文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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