Eager load while using Linq in NHibernate 3

泪湿孤枕 提交于 2019-11-30 15:10:02

问题


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;} 
    .
    .
    .
}

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.. 
    }
}

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?


回答1:


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);



回答2:


For me this thread solve problem.

Linq for NHibernate - filtering on <many-to-one> foreign key causes extra lookup

var linqsession = session.Linq<FeedItem>();
linqsession.QueryOptions.RegisterCustomAction(c => c.SetResultTransformer(new DistinctRootEntityResultTransformer()));
var feedItemQuery = from ad in linqsession.Expand("Ads")
                    where ad.Id == Id
                    select ad


来源:https://stackoverflow.com/questions/2075048/eager-load-while-using-linq-in-nhibernate-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!