EF Core,使用包含或再次上下文
EF Core, use include or again context
开始于:
"Eager loading a collection navigation in a single query may cause
performance issues."
参见:Source
并且建议使用带有 include 的拆分查询。我想知道是否不是包含在下面的查询中:
var task = await context.Tasks
.Include(x => x.TaskDependencies)
.Select(x => new TaskBaseModel
{
Id = x.Id,
Name = x.Name,
Description = x.Description,
TaskDependencies= x.TaskDependencies.ToArray()
})
.SingleOrDefaultAsync(x => x.Id == _id);
我应该这样做:
var task = await context.Tasks
.Select(x => new TaskBaseModel
{
Id = x.Id,
Name = x.Name,
Description = x.Description,
TaskDependencies= context.TaskDependencies
.Where(y => y.TaskId == x.Id).ToArray()
})
.SingleOrDefaultAsync(x => x.Id == _id);
有人知道这方面的信息吗?关于性能等..
此致
两个查询应该具有相同的性能 SQL。请注意,Include
后跟 Select
会被 EF Core 忽略。
所以,最舒服的查询是:
var task = await context.Tasks
.Select(x => new TaskBaseModel
{
Id = x.Id,
Name = x.Name,
Description = x.Description,
TaskDependencies = x.TaskDependencies.ToArray()
})
.SingleOrDefaultAsync(x => x.Id == _id);
开始于:
"Eager loading a collection navigation in a single query may cause performance issues."
参见:Source
并且建议使用带有 include 的拆分查询。我想知道是否不是包含在下面的查询中:
var task = await context.Tasks
.Include(x => x.TaskDependencies)
.Select(x => new TaskBaseModel
{
Id = x.Id,
Name = x.Name,
Description = x.Description,
TaskDependencies= x.TaskDependencies.ToArray()
})
.SingleOrDefaultAsync(x => x.Id == _id);
我应该这样做:
var task = await context.Tasks
.Select(x => new TaskBaseModel
{
Id = x.Id,
Name = x.Name,
Description = x.Description,
TaskDependencies= context.TaskDependencies
.Where(y => y.TaskId == x.Id).ToArray()
})
.SingleOrDefaultAsync(x => x.Id == _id);
有人知道这方面的信息吗?关于性能等..
此致
两个查询应该具有相同的性能 SQL。请注意,Include
后跟 Select
会被 EF Core 忽略。
所以,最舒服的查询是:
var task = await context.Tasks
.Select(x => new TaskBaseModel
{
Id = x.Id,
Name = x.Name,
Description = x.Description,
TaskDependencies = x.TaskDependencies.ToArray()
})
.SingleOrDefaultAsync(x => x.Id == _id);