Search code examples
c#.netnhibernatehibernate-mappinghbm

NHibernate enable lazy loading for single query


In .hbm mapping file, i have the following setting:

<set name="Fields" table="Fields" lazy="false" cascade="all" inverse="false">

/.../

The lazy loading for this collection is globally disabled

Is it a way to "locally" enable lazy loading only for single Linq query?

The HBM mapping file must remains intact.


Solution

  • Well, I don't think it is achievable by LINQ, but you can use NHibernate's ICriteria to achieve what you want. An example could be like below.

    ISession.CreateCriteria<Entity>()
            .SetFetchMode("Fields", FetchMode.Lazy)
            .List<Entity>();
    

    More information about ICriteria can be found: https://nhibernate.info/doc/nhibernate-reference/querycriteria.html