简体   繁体   English

在Get()之后急于加载关联

[英]Eagerly loading an association after Get()

I am trying to find a better way to load the relation than this: 我正在尝试找到一种更好的方式来加载关系:

result = session.Get<Author>(id);
Course course = result.Courses.FirstOrDefault();

I can do this with QueryOver API like this: 我可以使用QueryOver API做到这一点,如下所示:

result = session.QueryOver<Author>()
                .Where(item => item.Id == id)
                .Fetch(item => item.Courses).Eager
                .SingleOrDefault();

I guess it would generate the same SQL but it is too verbose. 我猜它会生成相同的SQL,但是太冗长了。

Is there a way to do something like below? 有没有办法做下面的事情?

session.Fetch(result, author => author.Courses);

Get is driven by mapping. 获取由映射驱动。 If it really make sense, change your mapping (but I would not do that). 如果确实有意义,请更改您的映射(但我不会这样做)。 There is no runtime switch of constructed mapping. 没有构造映射的运行时切换。

From my experience, few more select statements during the Get(id) is not an issue... And for N + 1 you've already shown the better solution in your question. 根据我的经验,在Get(id)期间没有更多的select语句不是问题...而且对于N + 1,您已经展示了问题中的更好的解决方案。

Interesting reading about eagar loading: http://nhforge.org/wikis/howtonh/lazy-loading-eager-loading.aspx 有趣的阅​​读,关于鱼装载: http ://nhforge.org/wikis/howtonh/lazy-loading-eager-loading.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM