74

NHibernate eager loading can be done using Fetch and FetchMany, as described in NHibernate Linq Eager Fetching on Mike Hadlow's blog.

What is the difference between these two methods and under what circumstance would each be used?

1 Answer 1

107

Fetch should be used for references and FetchMany for collections.

This is particularly important because only FetchMany can be combined with ThenFetchMany to fetch "grandchildren" collections.

Example:

session.Query<User>() .FetchMany(u => u.Orders) .ThenFetchMany(o => o.OrderItems) 
Sign up to request clarification or add additional context in comments.

3 Comments

Is there a problem using Fetch() on collections if you're not planning on calling ThenFetch() afterwards?
@Diego Mijelshon since both Fetch and FetchMany work, what is the difference?
@hardywang: the way they can be chained to fetch more levels. Just look at the method signatures.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.