EF CTP5选择哪个代码生成项?

发布于 2024-10-20 04:58:10 字数 980 浏览 2 评论 0原文

随着 EF 4.1 即将推出以及 CTP5 的推出已有几个月,我决定尝试一下新功能。正如我所看到的,有多个可用的生成项(DbContext 和三个不同的 ObjectContext)。我还注意到它们不可互换 - 我第一次在我的一个应用程序中使用 POCO ObjectContext,今天切换到 DbContext,我的整个存储库都崩溃了。它基于 LoadProperty() 方法、DeleteObject()AddObject() 方法,而这些在 DbSet 上都缺失。 code> 类,用于 DbContext 生成。

我知道这里有一个很棒的博客系列 http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part -1-introduction-and-model.aspx 介绍了新功能,但它从未真正说明何时选择什么。

我的要求是:

  • ASP.NET MVC应用程序,所以很懒 加载大部分不起作用,因为在页面上 渲染它会说上下文有 已经被处置了(这就是为什么我 需要简单的显式支持 加载 - 在 EF4 中我是通过 Include(),使用我所做的 POCO 上下文 通过 LoadProperty() 现在 在 DbContext 我相信我会使用 强类型 Include())。
  • 我们可能不需要代码优先 功能(但你永远不知道)。

With EF 4.1 on the way and CTP5 being available for few months now I've decided to try out the new functionality. As I see, there are multiple generation items available (DbContext and three different ObjectContext's). I also noticed they are not interchangable - I was first using the POCO ObjectContext in one of my app and today switched to DbContext and my entire repository broke. It was based on LoadProperty() methods, DeleteObject() and AddObject() methods and those are all missing on DbSet class which is used in DbContext generation.

I know there's a great blog series here http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-1-introduction-and-model.aspx introducing the new functionality but it never really says when to choose what.

My requirements are:

  • ASP.NET MVC application, so lazy
    loading mostly won't work coz at page
    render it will say that context has
    already been disposed (that's why I
    need easy support for explicit
    loading - in EF4 I did it via
    Include(), using POCO context I did
    it through LoadProperty() and now
    in DbContext I believe I will use
    the strongly typed Include()).
  • We probably won't be needing code-first
    features (but you never know).

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

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

发布评论

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

评论(2

沧笙踏歌 2024-10-27 04:58:10

这两者之间的区别主要在于 API 和功能集DbContext 当然有用于查询和 LoadInclude,但您会在其他地方找到它。此外,当使用 CTP5 程序集时,您将为 ObjectSetDbSet 提供强类型 Include (可在 IQueryable 接口上作为扩展方法使用)。

显式加载(相当于 LoadProperty)由 DbReferenceEntryDbCollectionEntry 上的 Load 方法执行- 检查显式加载相关实体。它比 LoadProperty 工作得更好,因为您可以定义加载过滤器。

Difference between those two is mostly in API and feature set. DbContext of course have Include for query and Load but you will find it elsewhere. Moreover when using CTP5 assembly you will have strongy typed Include for both ObjectSet and DbSet (available on IQueryable interface as extension method).

Explicit loading (equivalent to LoadProperty) is performed by Load method on DbReferenceEntry<T> or DbCollectionEntry<T> - check Explicit loading related entities. It works even better then LoadProperty because you can define filter for loading.

甜扑 2024-10-27 04:58:10

您一开始就错误地认为不能在 MVC 中使用延迟加载。

如果您在更高级别上管理上下文,那么您将能够毫无问题地做到这一点。

You are starting with the wrong assumption that you can't use lazy load with MVC.

If you manage the context at a higher level, you'll be able to do that without problems.

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