实体框架 4 - 存储库模式 - 如何避免具有嵌套关系的实体的完全加载
我目前正在使用 Entity Framework 4 和 c# 中的存储库模式。
我有一个实体(Category
),其中包含一个属性(Main Category
),该属性引用主类别和其他一些内容。
由于导航属性,主类别可以访问其子类别。
出于稍后解释的原因,我禁用了延迟加载。
当我尝试获取具有其 id 的类别并且由于导航属性时,EF4 会加载整个图表,而我只想加载 >目标类别并且不是相关(父或子)实体。
我想这样做是因为该实体旨在转换为 dto 并在 WCF WebService 中使用。图表的全部负载与我的方法不兼容。
有没有办法避免使用 EF4 加载嵌套实体?如果不是,NHibernate 可以这样工作吗? (我认为禁用延迟加载将允许我完全控制我想要加载的内容(显式或急切加载),但似乎不是......)
I'm currently working with Entity Framework 4 and the repository Pattern in c#.
I have an entity (Category
) which contains a property (Main Category
) which refers to the Main Category and some other stuff.
Because of navigation property, the main category has access to their childs category.
For reasons I'll explain later, I disabled lazy loading.
When I try to get a category with his id and because of navigation properties, EF4 load the whole graph whereas I just want to load the targeted category and not the related (parent or child) entity.
I want to do this because this entity is aimed to be converted into a dto and used in a WCF WebService. The full load of graph is not compatible with my approach.
Is there a way to avoid loading of nested entity with EF4? If not, can NHibernate be made to work that way? (I was thinking that disabling lazy loading will allow me to have full control on what I want to load (explicit or eager loading) but it seems not ...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您从对象上下文本身获取结果时,为什么不将实体对象投影到该 dto 对象中?您可以使用类似的方法
来返回
clientDTO
对象的Ienumerable
。Why don't you project your entity object into that dto object when you get the results from the object context itself? You can use something like
which will return the
Ienumerable
of theclientDTO
object.