Ninerbate延迟加载参考实体 [英] Nhinerbate lazy loading of reference entity

查看:98
本文介绍了Ninerbate延迟加载参考实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况:

class A
{
    public virtual int Id { get; set; }
    public virtual B Child { get; set; }
}

class B
{
    public virtual int Id { get; set; }
}

在类A的映射中,我引用了类B:

In the mapping of class A, I have a reference to class B:

map.Reference(a => a.Child).LazyLoad();

现在,当我执行以下操作时:

Now when I do something like:

Session.Query<TypeOfA>().Select(a => a);

除了从ATable中正常选择*外,我还从BTable中为每条A行获得n个选择.就像延迟加载无法正常工作.

Apart from the normal select * from ATable I get n selects from the BTable for each A line. Is like lazy loading is not working.

我的问题是:

  1. 如何使lazyload在这里工作?
  2. 我可以将A实体和B实体带入一个查询中吗?

谢谢

推荐答案

默认情况下,延迟加载是打开的,并且应该可以正常工作.如果存在问题(例如,如果它无法生成类B的代理),则在创建会话工厂时会抱怨.

Lazy loading is switched on by default and should actually work. If there would be a problem, for instance if it can't generate the proxy for class B, it would complain when creating the session factory.

您确定对B的查询是由查询本身完成的,而不是随后对A的访问吗?

Are you sure that the queries for B are done by the query itself, and not be subsequent access to A?

您可以通过两种方式优化对B的访问:在单个查询中将它们与A一起获取. (我不懂流利的语言,这是配置它的xml方法:)

You could optimize the access to B in two ways: fetch them together with A in a single query. (I don't know fluent, this is the xml way to configure it:)

<many-to-one fetch="join" ...>

与列表一起使用时,这会遇到一些问题,也可能使您的查询大打折扣.当然,这根本不是延迟加载.

This has some problems when used with lists and could also blow up your query a lot. It is of course not lazy loading at all.

另一种非常不错且功能强大的优化是批量获取.它允许在单独的查询中提取实例,但一次提取其中几个实例.

Another, very nice and powerful optimization is batch fetching. It allows the instances to be fetched in separate queries, but fetches several of them at once.

<class name="B" batch-size="20" ...>

这将在一个查询中一次获取20个B.它也可用于列表:

This would fetch 20 B's at once in one query. It is also available for lists:

<one-to-many fetch-size="20" ...>

这篇关于Ninerbate延迟加载参考实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆