EF7投影犯规渴望负荷收藏 [英] EF7 projection doesnt eager load collections

查看:181
本文介绍了EF7投影犯规渴望负荷收藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在选择具有包括我的所有项目获取与单个SQL获取实体连接语句。但是,当我把它投射到一些其他形式与其子,连接不再执行,而不是每行一个单独的查询执行获得孩子。我怎样才能prevent呢?我的目标是减少列读取的,并减少查询的量

这个问题使我相信,这应该工作: https://github.com/aspnet /的EntityFramework /问题/ 599

  //执行一个查询如预期
context.Parents.Include(p值=> p.Children).ToList();//执行多个查询
context.Parents.Include(P => p.Children)。选择(P =>新建{
    ID = p.Id,
    名称= p.Name,
    儿童= p.Children.Select(C =>新建{
        ID = c.Id,
        名称= c.Name
    })
})了ToList()。


解决方案

您看到发送到数据库的多个查询,因为EF核心还没有足够的智慧在投影到一个JOIN翻译导航。这里是问题跟踪此功能 - https://github.com/aspnet/EntityFramework/issues/4007

BTW由他人pviously提到的$ P $,仅包括工作时,如果结果的一部分(即它的意思是如果你最终创建实体类型的实例,那么要确保这个导航属性​​填充)的实体类型

When selecting entities with "include" all my items gets fetched with a single SQL join statement. But when i project it to some other form with its children, the join is no longer executed, instead a separate query per row is executed to get the children. How can i prevent this? My goal is to reduce the columns fetched, and to reduce the amount of queries

This issue leads me to believe that this should work: https://github.com/aspnet/EntityFramework/issues/599

//executes ONE query as expected
context.Parents.Include(p => p.Children).ToList();

//executes MULTIPLE queries
context.Parents.Include(p => p.Children).Select(p => new {
    Id = p.Id,
    Name = p.Name,
    Children = p.Children.Select(c => new {
        Id = c.Id,
        Name = c.Name
    })
}).ToList();

解决方案

You are seeing multiple queries sent to the database because EF Core is not yet smart enough to translate navigations in a projection to a JOIN. Here is the issue tracking this feature - https://github.com/aspnet/EntityFramework/issues/4007.

BTW as previously mentioned by others, Include only works when the entity type if part of the result (i.e. it means "if you end up creating instances of the entity type then make sure this navigation property is populated").

这篇关于EF7投影犯规渴望负荷收藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆