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.

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top