nHibernate Eager在运行时加载 [英] nHibernate Eager Loading at runtime
问题描述
$ b
为了达到这个目的,我们做了以下工作:
var iSession = unitOfWorkScope。 CurrentUnitOfWork< NHUnitOfWork>()的getSession< ParentEntity>();
iSession.CreateCriteria< ParentEntity>()。SetFetchMode(Children,FetchMode.Eager);
在会话中设置好后,我们使用NCommon的IRepository功能来检索实体:
var parent = parentRepository.FirstOrDefault(x => x.Id == 123);
但是,当我们检查parent.Children集合时,我们得到:
NHibernate.Collection.Generic.PersistentGenericBag< ChildEntity>
...这告诉我没有发生Eager-loading。当我们将NH映射修改为始终强制加载时,我们会正确地看到这些孩子。
任何人都可以解释这一点吗?由于FetchMode.Eager被忽略,我们假设我们错过了一些东西。
这是解决方案, p>
http://slynetblog.blogspot.com/2011/11/in-spite-of-common-now-approach-of.html
We're using NCommon's UnitOfWorkScope which wraps nHibernate ISession functionality. Our goal is to eager-load complex properties on demand vs. always eagerly loading them via configuration. The idea is that a given service that retrieves an entity can be customized a bit by the calling code - sometimes we want only the parent entity to be hydrated, other times we may want the complex child properties hydrated, too.
To accomplish this, we're doing the following:
var iSession = unitOfWorkScope.CurrentUnitOfWork<NHUnitOfWork>().GetSession<ParentEntity>();
iSession.CreateCriteria<ParentEntity>().SetFetchMode("Children", FetchMode.Eager);
Once that's setup on the session, we use NCommon's IRepository functionality to retrieve the entities:
var parent = parentRepository.FirstOrDefault(x => x.Id == 123);
However, when we check the parent.Children collection we get:
NHibernate.Collection.Generic.PersistentGenericBag<ChildEntity>
... which tells me the Eager-loading did not occur. When we modify the NH mapping to always force an eager load, then we see the children correctly.
Can anyone shed light on this? I assume we're missing something since the FetchMode.Eager is ignored.
This was the solution, found here:
http://slynetblog.blogspot.com/2011/11/in-spite-of-common-now-approach-of.html
这篇关于nHibernate Eager在运行时加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!