FluentNHibernate: LazyLoad and Fetch

2019-07-31 01:20发布

问题:

in fluent nhibernate I can set Fetch.Something and Not.LazyLoad to a Reference or HasMany. What will happen if I use both?

How these two reflects to querying data in these three ways?

class UserMap
{
   HasMany(x=>x.Contacts). (Fetch or Not.LazyLoad)
   References(x=>x.Supervisor). (Fetch or Not.LazyLoad)
}

session.Query<User>();
session.Query<User>().FetchMany(x=>x.Contacts);
session.Get<User>(ID);

回答1:

The problem is that Fetch is not taken into account for Query/HQL. So, immediately after running the query, it will try to fetch your Not.LazyLoad properties one by one.

In general, disabling lazy loading is a bad idea in 99% of the cases. Suggested read: NHibernate is lazy, just live with it