EF4:为什么代理的创建具有启用时延迟加载启用? [英] EF4: Why does proxy creation have to be enabled when lazy loading is enabled?

查看:213
本文介绍了EF4:为什么代理的创建具有启用时延迟加载启用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目结构如下:
.Persistence - >的.repo - > .Services - > .Controllers - > MVC3应用。



每个层具有接口各组装还有还有一些其他的组件一样.Entities,.ViewModels和通用代码集。



持久性 - 这个拥有的EF4的datacontext(代码优先)和EF4.3的参考。有一个工厂,创建一个名为的getContext()Context和这家工厂实现IDisposable。这不是一个单身的原因我想这就是温莎会为我做LifestyleSingleton()结果
回购 - 这个拥有仓库实施库和规范模式(的 HTTP ://huyrua.wordpress.com/2010/07/13/entity-framework-4-poco-repository-and-specification-pattern/ )。



其它层是不言自明...



问题: < BR>
1.为什么代理的创建已被当启用延缓加载启用?结果
2.如果我想设置惰性加载=假的,我可以投我的IEnumerable在服务层的ObjectQuery为了使用.INCLUDE()呢?


解决方案

为什么代理的创建已启用当延迟加载启动?




由于延迟加载与波苏斯依赖于代理的创建。如果没有代理延迟加载不起作用。因此,结合 ProxyCreationEnabled = FALSE LazyLoadingEnabled = TRUE 是没有意义的。反向组合是有道理的,如果你想与更改跟踪代理工作,但不想使用延迟加载。




如果我要设置惰性加载=假,可我投我的IEnumerable在服务层的ObjectQuery才能使用.INCLUDE()呢?




这取决于你的的IEnumerable< T> 真的的。如果它是一个了ToList()则没有(因为名单,LT的结果; T> 是一个执行的IEnumerable< T> 但不是的IQueryable< T> )。如果你只是返回的IQueryable< T> 的IEnumerable< T> 你或许可以转换为 IQueryable的< T> 。 (在EF 4.3你可以使用的IQueryable< T> 的DBQuery< T> ,而不是的ObjectQuery< T>



但恕我直言,需要这样一个投表示,什么是错在你的架构。使用包含是查询的修改。如果您的服务层允许修改查询你的资料库应该返回的IQueryable< T> - 这种类型的建筑和修改的查询作出



如果你的仓库不应该返回的IQueryable< T> 你必须通过在表达式或规范成用来添加库的方法包含来查询 - 内库的方法,而不是在服务层


I have a project structured as follows: .Persistence -> .Repo -> .Services -> .Controllers -> MVC3 App.

Each layer has a respective assembly with interfaces and there's also some other assemblies like .Entities, .ViewModels, and common code assemblies.

Persistence - This holds the EF4 datacontext (code-first) and a reference to EF4.3. There's a factory for creating the Context called GetContext() and this factory implements IDisposable. It is NOT a singleton cause I figured that's what windsor would do for me with LifestyleSingleton().
Repo - This holds repositories implementing the repository and specification pattern (http://huyrua.wordpress.com/2010/07/13/entity-framework-4-poco-repository-and-specification-pattern/).

The other layers are self-explanatory...

Questions:
1. Why does proxy creation have to be enabled when lazy loading is enabled?
2. If I want to set lazyloading = false, can I cast my IEnumerable in Service layer to ObjectQuery in order to use .Include() there?

解决方案

Why does proxy creation have to be enabled when lazy loading is enabled?

Because lazy loading with POCOs relies on proxy creation. Without proxies lazy loading doesn't work. Therefore the combination ProxyCreationEnabled = false and LazyLoadingEnabled = true makes no sense. The reverse combination makes sense, in case you want to work with change tracking proxies but don't want to use lazy loading.

If I want to set lazyloading = false, can I cast my IEnumerable in Service layer to ObjectQuery in order to use .Include() there?

It depends on what your IEnumerable<T> really is. If it's the result of a ToList() then no (because List<T> is an implementation of IEnumerable<T> but not of IQueryable<T>.). If you just return an IQueryable<T> as IEnumerable<T> you probably can cast to IQueryable<T>. (In EF 4.3 you would use IQueryable<T> or DbQuery<T> rather than ObjectQuery<T>.)

But imho the need for such a cast indicates that something is wrong in your architecture. Using Include is a modification of a query. If your service layer is allowed to modify queries your repository should return IQueryable<T> - this type is made for building and modifying queries.

If your repository is not supposed to return IQueryable<T> you must pass in an expression or a specification into the repository methods which is used to add an Include to your query - inside of the repository method, not in the service layer.

这篇关于EF4:为什么代理的创建具有启用时延迟加载启用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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