nHibernate Collection Count

2019-06-22 00:52发布

I have the following model which I have created and mapped with nHibernate. Using lazy loading so I don't need to get the Vehicles for the Dealer at the start.

Public class Dealer
{
public virtual string Name { get;set;}
public virtual IList<Vehicles> Vehicles { get;set;}
}

Now let's assume the Dealer has thousands of vehicles.

If I do Dealer.Vehicles.Count then NH will select and pull all the data.

What is the best way to simply get a count? Is there any way in which I can get a count with out declaring A new property dealerCount within the Dealer Class?

Also there is a feature in Hibernate which I believe will be implemented in a newer version of NH called Extra Lazy Loading. Would this solve the problem?

2条回答
仙女界的扛把子
2楼-- · 2019-06-22 01:18

Use count projection (Projections.RowCount)

查看更多
▲ chillily
3楼-- · 2019-06-22 01:37

extra lazy loading would issue sql instead of populating the collection for certain operations such as Count or Contains. In fluent mappings its used as:

HasMany(x => x.CollectionProperty).ExtraLazyLoad();

or HBM

<one-to-many lazy="extra" ...

It's only usefull if you have large collections and need the special behavior.

查看更多
登录 后发表回答