【发布时间】:2018-08-27 00:16:53
【问题描述】:
我正在使用 EF Core 2.1 预览版,它本应减少 N+1 查询问题。 我正在尝试进行查询,选择带有帖子作者的论坛主题:
dbContext.ForumThreads
.Include(t => t.Posts)
.Take(n)
.Select(t => new
{
t.Id,
t.Title,
PostAuhtors = t.Posts.Select(p => p.Author).Take(5)
}).ToArray();
这会产生 n+1 个查询:对于每个 ForumThread,它会选择帖子作者
架构很简单:
public class ForumThread
{
public Guid Id {get;set;}
public string Title {get;set;}
public ICollection<ForumPost> Posts {get;set;}
}
public class ForumPost
{
public Guid Id {get;set;}
public string Author {get;set;}
public string Content {get;set;}
}
【问题讨论】:
标签: c# sql-server entity-framework-core asp.net-core-2.0