使用 T4 模板与 EF4.1 简化 API 模型优先方法从模型生成 POCO 类

发布于 2024-10-26 01:14:49 字数 681 浏览 1 评论 0原文

我有与视觉设计师一起创建的模型。现在我想要从中生成 POCO 类。在我的另一个问题中,有人向我建议了 EF4.1 简化 API 模型优先方法。之前我也在考虑T4模板。

与使用 T4 模板生成 POCO 类相比,EF4.1 简化 API Model First 有哪些限制? EF4.1 方法有什么优点(如果有的话)(除了它应该更易于使用)?我现在的决定将很难撤销,因为我将有相当大的持久层,所以将来可能很难对其进行更改。

特别是,我对这个功能很感兴趣:

  • 每次更改模型(在可视化设计器中)时,我是否可以获取这两个选项中的任何一个来生成数据库表,这样我就不必生成查询并运行它们?这将大大加快我的开发过程(由于某种原因,我每次更改模型时都必须手动删除表,因此需要花费大量时间)。我知道这可以通过
  • 我可以在另一个项目中拥有 POCO 课程来完成吗?我知道这可以是带有 T4 模板的圆顶,但是使用简化的 API 也可以吗?
  • 生成的 POCO 类中的属性是否可以进行注释,而不会在每次从模型重新生成时被覆盖? (这确实可以通过代码优先的方法实现)
  • 效率上有什么区别吗?

如果重要的话,所选择的任何技术都将在 ASP.NET MVC 应用程序中使用。


编辑: 如果您知道答案,请回答我的问题的任何子问题。也许与另一个部分答案一起它会给我我需要的信息。谢谢

I have model created with visual designer. Now I want to have POCO classes generated from it. In another question of mine, EF4.1 simplified API Model First approach was suggested to me. Before I was also thinking about T4 templates.

What are the limitation of EF4.1 simplified API Model First comparing to using T4 templates to generate POCO classes ? What are advantages to of EF4.1 approach if any (besides it is supposed to be simpler to use)? My decision now will be very hard to undone as I will have quite huge persistence layer, so it might be hard to make changes to it in future.

Particularly, I'm interested in this features:

  • can I get either of those 2 options to generate database tables every time I change model (in visual designer) so I don't have to generate queries and run them ? That would speed up my development process a lot (for some reason I have to drop tables manually every time I change model so it takes a lot of time). I know this can be done with
  • can I have POCO classes in another project ? I know this can be dome with T4 templates but is it also possible with simplified API?
  • can properties in generated POCO classes be annotated without being overwritten every time they gets regenerated from model ? (this is indeed possible with code-first approach)
  • is there any difference in efficiency?

If it matters any technology chosen will be used in ASP.NET MVC application.


EDIT:
Please answer any subquestion of my question if you know the answer. Maybe together with another partial answer it will give me the information I need. Thank you

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

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

发布评论

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

评论(1

oО清风挽发oО 2024-11-02 01:14:49

EF 4.1 提供了简化的 API,这主要是因为 EF 中首次提供了代码优先方法。您所描述的是模型优先方法。

我已经回答了类似的问题,但我会尝试回答您的个人问题:

  • DbContext API 的主要限制是缺乏信息。我们知道新功能,但我们仍然不知道如何实现 ObjectContext API 中可用的某些功能(简化可能删除了一些功能)。我不确定这与 DbContext 生成器 T4 模板的模型优先方法有何关系,因为在这种情况下,映射仍然在 EDMX 文件中定义,因此应该克服代码优先映射的一些众所周知的问题。
  • 使用 DbContext API 自动生成数据库。默认情况下,ObjectContext API 仅提供数据库脚本生成,您必须执行该脚本。另一方面,如果您有 VS 2010 Premium 或 Ultimate,则可以下载 Entity设计数据库生成电源包,它允许您增量构建数据库,而无需每次都删除它。目前 DbContext API 无法做到这一点。
  • 您可以使用这两个 API 在其他项目中拥有 POCO 类(模板)。
  • 是的,即使使用 T4 模板生成 POCO 类您也可以使用注释
  • 不,效率上没有区别。 DbContext API 只是 ObjectContext API 之上的包装器。

EF 4.1 provides simplified API mostly glorified because of the code first approach available in EF for the first time. What you describe is the model first approach.

I already answered similar question but I will try to answer your individual questions:

  • Main limitation of DbContext API is lack of information. We know new features but we still don't know how to achieve some features available in ObjectContext API (simplification probably removed some features). I'm not sure how this relate to the model first approch with DbContext generator T4 template because in this case mapping is still defined in EDMX file so some well known problems with the code first mapping should be overcome.
  • Generating database works automtic with DbContext API. ObjectContext API by default offers only db script generation and you must execute the script. On the other hand if you have VS 2010 Premium or Ultimate you can download Entity designer database generation power pack which will allow you building your database incrementaly without deleting it every time. That is something not possible with DbContext API at the moment.
  • You can have POCO classes (templates) in other project with both APIs.
  • Yes even with T4 template generating POCO classes you can use annotations.
  • No there is no difference in efficiency. DbContext API is just wrapper on top of ObjectContext API.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文