Fluent Nhibernate loading not falling back on lazy loading? (grandchild entities)

不打扰是莪最后的温柔 提交于 2019-12-12 04:18:27

问题


When querying nhibernate, I'm seeing some odd behavior

When I write a query like this

Repository.QueryOver<Entity>()
    .Fetch(x => x.Child1).Eager
    .Fetch(x => x.child2).Eager

It will eagerly grab child1 and child2 entities, but there are grandchildren for child1 and child2 that aren't lazily loaded. I'm a bit confused on how to achieve this.

In my nhibernate mappings, it seems to have no affect on the laziness or eagerness of grandchildren and I require at least some entities be eagerly loaded to avoid the N+1 query problem.

I'm also wondering how I could eagerly load grandchildren entities under my original entity.

Any help or ideas are appreciated!


回答1:


I would suggest to use the batch-fetching. As discussed here, the fluent syntax is:

1) the collection setting

HasMany<MyEntity>(x => x.Entities)
  .BatchSize(100);

2) the class level setting

public MyEntityMap()
{
    Id(x => x....
    ...
    BatchSize(100);

This setting should be applied on every collection and every class. To do that with fluent - we can use Conventions - see more e.g. here

'IClassConvention' - Use to alter your ClassMaps values. You can't change properties, collections, etc with this convention, only alter the settings such as Lazy and BatchSize.



来源:https://stackoverflow.com/questions/34138989/fluent-nhibernate-loading-not-falling-back-on-lazy-loading-grandchild-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!