鉴于此:
namespace TheEntities
{
[DataContract(IsReference=true)]
public class Question
{
[DataMember] public virtual int QuestionId { get; set; }
[DataMember] public virtual string Text { get; set; }
[DataMember] public virtual string Poster { get; set; }
[DataMember] public virtual IList<QuestionComment> Comments { get; set; }
[DataMember] public virtual IList<Answer> Answers{ get; set; }
[DataMember] public virtual byte[] RowVersion { get; set; }
}
[DataContract]
public class QuestionComment
{
[DataMember] public virtual Question Question { get; set; }
[DataMember] public virtual int QuestionCommentId { get; set; }
[DataMember] public virtual string Text { get; set; }
[DataMember] public virtual string Poster { get; set; }
}
[DataContract(IsReference = true)]
public class Answer
{
[DataMember] public virtual Question Question { get; set; }
[DataMember] public virtual int AnswerId { get; set; }
[DataMember] public virtual string Text { get; set; }
[DataMember] public virtual string Poster { get; set; }
[DataMember] public virtual IList<AnswerComment> Comments { get; set; }
}
[DataContract]
public class AnswerComment
{
[DataMember] public virtual Answer Answer { get; set; }
[DataMember] public virtual int AnswerCommentId { get; set; }
[DataMember] public virtual string Text { get; set; }
[DataMember] public virtual string Poster { get; set; }
}
}
实体框架在NHibernate的同时没有为Answer,QuestionComment,AnswerComment生成重复的对象。
public Question OpenQuestion(int id)
{
var repo = QuestionRepository;
var query = repo.All.Where(y => y.QuestionId == id);
if (QuestionRepository.GetType() == typeof(EfRepository<Question>))
{
query = query
.Include("Answers")
.Include("Answers.Comments")
.Include("Comments");
return query.Single();
}
else if (QuestionRepository.GetType() == typeof(NhRepository<Question>))
{
// kinda sad, produces duplicate objects
query = query
.FetchMany(x => x.Answers)
.ThenFetchMany(x => x.Comments)
.FetchMany(x => x.Comments);
}
else
throw new Exception("Something unsupported");
return query.Single();
}
这也会产生重复的对象(深度为三级,使用三种关系):
query = query
.FetchMany(x => x.Answers)
.ThenFetchMany(x => x.Comments)
这也会产生重复的对象(仅限两个级别,但使用三个关系):
query = query
.FetchMany(x => x.Answers)
.FetchMany(x => x.Comments);
这不会产生重复的对象,但是热切的加载仅针对两个级别的深度和两个关系,即从问题到答案。对于要回答的评论和要回答的评论,它们是在单独的查询中执行的。
query = query
.FetchMany(x => x.Answers);
如果NHibernate只能在两个级别FetchMany上完成它的工作,只有两个关系,为什么还要创建ThenFetchMany(在三个级别上使用,但有错误,有重复的对象)?事实上,如果你想在三个关系中使用它,即使FetchMany也是无用的,它也会产生重复的对象。
NHibernate团队是否仍然不愿意删除ThenFetchMany,因为它无法正常工作?
我的映射没有错误,当我删除了抓取策略时,事情正常工作(即不会产生重复的对象)。
答案 0 :(得分:6)
获得独特的结果
对于Linq:
.Distinct()
用于QueryOver
.TrasformUsing(Transformers.DistinctRootentity)
for Criteria
.SetResulttransformer(Transformers.DistinctRootentity)
编辑:有效地这是NH的一个缺点,它将发行笛卡尔积,但这可以改进
repo.All.Where(y => y.QuestionId == id)
.FetchMany(x => x.Answers)
.ThenFetchMany(x => x.Comments)
.Future()
query = repo.All.Where(y => y.QuestionId == id)
.FetchMany(x => x.Comments)
var result = query.AsEnumerable().Single();
请参阅http://ayende.com/blog/4367/eagerly-loading-entity-associations-efficiently-with-nhibernate
它看起来有点奇怪,但应该做
答案 1 :(得分:0)
尝试:
query.QueryOptions.RegisterCustomAction(c => c.SetResultTransformer(new DistinctRootEntityResultTransformer()));