EF CTP5选择哪个代码生成项?
随着 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 viaInclude()
, using POCO context I did
it throughLoadProperty()
and now
inDbContext
I believe I will use
the strongly typedInclude()
). - We probably won't be needing code-first
features (but you never know).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这两者之间的区别主要在于 API 和功能集。
DbContext
当然有用于查询和Load
的Include
,但您会在其他地方找到它。此外,当使用 CTP5 程序集时,您将为ObjectSet
和DbSet
提供强类型Include
(可在 IQueryable 接口上作为扩展方法使用)。显式加载(相当于
LoadProperty
)由DbReferenceEntry
或DbCollectionEntry
上的Load
方法执行- 检查显式加载相关实体。它比 LoadProperty 工作得更好,因为您可以定义加载过滤器。Difference between those two is mostly in API and feature set.
DbContext
of course haveInclude
for query andLoad
but you will find it elsewhere. Moreover when using CTP5 assembly you will have strongy typedInclude
for bothObjectSet
andDbSet
(available on IQueryable interface as extension method).Explicit loading (equivalent to
LoadProperty
) is performed byLoad
method onDbReferenceEntry<T>
orDbCollectionEntry<T>
- check Explicit loading related entities. It works even better thenLoadProperty
because you can define filter for loading.您一开始就错误地认为不能在 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.