Home > Back-end >  EF Core, use include or again context
EF Core, use include or again context

Time:09-29

Since:

"Eager loading a collection navigation in a single query may cause performance issues."

see: Source

And it is advise to use split queries with include. I wonder if instead of include in the query bellow:

            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);

I should do this:

                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);  

Anyone as any info regarding this? about performance, etc..

Regards

CodePudding user response:

Both queries should have the same performance and SQL. Note that Include followed by Select is ignored by EF Core.

So, most comfortable query is:

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);
  • Related