在NHibernate 3中使用Linq时负载过大 [英] Eager load while using Linq in NHibernate 3
问题描述
在NHibernate 3主干版本的Linq急切加载时,我需要帮助.
I need help with eager loading in with Linq in NHibernate 3 trunk version.
我有很多这样的关系:
public class Post
{
public int Id {get;set;}
public IList<Tag> Tags { get;set;}
.
.
.
}
现在我在Fluent NHibernate中具有以下映射
Now I have the following mapping in Fluent NHibernate
public class PostMap:ClassMap<Post>
{
public PostMap()
{
Table("Posts");
Id(x => x.Id);
.
.
HasManyToMany(x => x.Tags)
.Table("PostsTags")
.ParentKeyColumn("PostId")
.ChildKeyColumn("TagId")
.Not.LazyLoad(); // this is not working..
}
}
现在,在获取帖子时,我还需要标签以渴望加载.我知道使用Criteria API和HQL是可能的,我应该使用SetFetchMode.但是在使用Linq时是否可以使用SetFetchMode? p>
Now while fetching the posts, I need the Tags also to eager load. I know that it is possible with Criteria API and HQL and the SetFetchMode is what I should use. But is there are way to use SetFetchMode when using Linq?
推荐答案
对此的支持早在某个时候就已经出现了.语法就像
Support for this went into the trunk sometime ago; the syntax is be something like
var query = session.Query<Post>().Where(bla bla).Fetch(p => p.Tags);
如果标签又有其他关系,则可以执行以下操作:
If Tags in turn had another relationship, you can do:
var query = session.Query<Post>().Where(bla bla).Fetch(p => p.Tags).ThenFetch(t => t.SomethingElse);
这篇关于在NHibernate 3中使用Linq时负载过大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!