Question

Started using NHibernate not very long ago. Have used EF for many years.

As my understanding LazyLoading is only load necessary data when needed. Collections should keep as queryable until ToList() or something similar called.

However, just found out when I use collection property inside linq query, nhibernate actually load the full collection as list before add query on top.

For example: User.Items.Count();

Nhibernate load full items list before count. As far as I know EF will only call database for the count not the full items list.

Can someone explain why? and how to keep collection property as queryable until needed?

Thanks a lot.

Was it helpful?

Solution

NHibernate loads full collection because it supports different cascading options for collections.

You will not be able to store bag or set as queryable for later SQL to be executed. The only optimization you can do is to set lazy="extra" that will translate your Count, Contains queries to appropriate SQL, but not LINQ.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top