简体   繁体   English

使用Include和AsNoTracking时查询中的N + 1选择

[英]N+1 selection in query when use Include and AsNoTracking

I have next relation (just example) User 0/1 - * Story I make next query when want to get list of stories with included users . 我有下一个关系(仅作为示例)用户0/1-*故事当我想获取包含用户的故事列表时,我进行下一个查询。

context.Stories.Include(s => s.User).AsNoTracking().ToList()

I noticed that ef makes additional requests to db for getting users (but this shouldn't be since I use include). 我注意到ef向db发出了其他请求以获取用户(但这不应该,因为我使用include)。 All works fine without AsNoTracking(). 没有AsNoTracking(),一切都可以正常工作。

Looks that issue with nullable relations since relations that dont allow null works fine. 看起来存在可空关系的问题,因为不允许空的关系可以正常工作。

Does anyone have similar issues? 有人有类似的问题吗? Probably it is expected behaviour in case of "allow null" relation. 在“允许为空”关系的情况下,可能是预期的行为。

You can explicitly turn off Lazy Loading as below: 您可以如下所示显式关闭“延迟加载”:

db.Configuration.LazyLoadingEnabled = false;
db.Configuration.ProxyCreationEnabled = false;            

EF will then not lazily load any further entities. 然后,EF将不会延迟加载任何其他实体。 You should check if you aren't inadvertently lazily loading some other linked entities. 您应该检查是否没有无意地延迟加载其他链接的实体。

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

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