在 NHibernate 3 中使用 Linq 时急切加载 [英] Eager load while using Linq in NHibernate 3

查看:18
本文介绍了在 NHibernate 3 中使用 Linq 时急切加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助在 NHibernate 3 主干版本中使用 Linq 进行急切加载.

I need help with eager loading in with Linq in NHibernate 3 trunk version.

我有这样的多对多关系:

I have a many-to-many relationship like this:

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?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆