简体   繁体   English

NHibernate提取创建正确的查询,然后执行N + 1

[英]NHibernate fetch creates right query and then performs N+1

I have three entities, configured as 我有三个实体,配置为

Contact
  HasOne(e => e.User).PropertyRef(e => e.Contact).Cascade.All();

User
  HasMany(e => e.Requests);
  References(e => e.Contact);

Request
  References(e => e.User);

And then I do query like this: 然后我这样查询:

  CurrentSession.Query<Request>()
                .Fetch(x => x.User)
                .ThenFetch(x => x.Contact)
                .ToList();

And it looks like Fetch is working, because in first select I see all the tables joined (omitted other fields for brevity): 看起来Fetch正在运行,因为在第一个选择中,我看到了所有连接的表(为简洁起见,省略了其他字段):

SELECT request0_.Id                 as Id128_0_,
   user1_.Id                       as Id131_1_,
   contact2_.Id                    as Id77_2_,
FROM   requests request0_
   left outer join users user1_
     on request0_.user_id = user1_.Id
   left outer join contacts contact2_
     on user1_.contact_id = contact2_.Id

And then follow N+1 selects for some reason, I can't understand and have no ideas how to troubleshoot the issue: 然后由于某些原因遵循N + 1选择,我不明白,也不知道如何解决此问题:

SELECT user0_.Id                as Id131_0_,
FROM   users user0_
WHERE  user0_.contact_id = 200 /* @p0 - contact_id */

目前,这是NHibernate中的错误

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

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