问题描述
我使用NHibernate的,我想手动控制获取相关的实体。
I am using NHibernate and I want to control fetching related entities manually.
下面是我的示例实体
public class Post
{
public virtual long Id { get; set; }
public virtual string Title { get; set; }
public virtual User User { get; set; }
public virtual IList<Like> Likes { get; set; }
public virtual IList<Tag> Tags { get; set; }
}
我希望behvaiour如下:
The behvaiour I expect is as follows:
session.Query<Post>().ToList();
在这种查询我想发表
实体有:
After this kind of query I want Post
entities to have:
- 原始属性设置
-
用户
属性不为null,但只有Id属性集。 -
喜欢
和标签
为null或空集
- Primitive properties are set
User
property is not null but only have Id property set.Likes
andTags
are null or empty collection
-
session.Query<Post>()
.Fetch(p => p.User)
.Fetch(p => p.Tags)
.ToList();
和这种查询我想以后发表
实体有:
And after this kind of query I want Post
entities to have:
- 原始属性设置
-
用户
属性不为null和属性进行设置。 -
标签
不为空,所有项目已全部属性设置 -
喜欢
为空或空集
- Primitive properties are set
User
property is not null and properties are set.Tags
is not null and all items have all properties setLikes
is null or empty collection
基本上我从NHibernate的希望是什么,而不是除非我要求它取回并不会导致NHibernate的特定异常(惰性初始模式等)获取任何相关的实体,当我尝试访问未读取性能。我期望的行为是不懒,也不急于。
Basically what I want from NHibernate is, not to fetch any related entities unless I ask for it to fetch and not cause an NHibernate specific exception (LazyInitialization etc.) when I try to access not fetched properties. The behaviour I expect is not lazy nor eager.
在你尝试过什么的评论,我试着用 LazyLoad几乎所有组合()
,未
,取
等。
Before "what have you tried" comments, I tried almost all combinations with LazyLoad()
, Not
, Fetch
etc. in Fluent NHibernate mapping configuration along with both statless and stateful sessions.
推荐答案
我无法弄清楚如何处理未初始化的实例,而不调用validate前参考:
I can't figure out how to deal with uninitialized instances without validate before call a reference:
NHibernateUtil.IsInitialized(entityOrCollection)
和/或
NHibernateUtil.IsPropertyInitialized(obj, "propertyName")
这篇关于NHibernate的手动控制取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!