左外连接实体框架的核心 [英] Left Outer Join with Entity Framework Core

查看:113
本文介绍了左外连接实体框架的核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图执行一个左外连接与EF7(7.0.0-RC1决赛)的要求,vNext RC1(RC1决赛)和SQL Server 2014年



< BLOCKQUOTE>

数据库:



宠物:ID,名称



网友:身份证,名称,#PetId




这一件作品:

  VAR queryWorks从u =在_context.Users 
加入在u.PetId _context.Pets p等价于p.Id到第
从对中pp.DefaultIfEmpty()
选择新{
=用户名u.Name,
宠物= p
};



但这个不起作用(消息=序列不包含任何元素):

  VAR queryFails =从u在​​_context.Users 
加入p在上u.PetId _context.Pets等于p.Id到页从对
在pp.DefaultIfEmpty()
选择新{
=用户名u.Name,
PetName =(p == NULL空:?p.Name)
};



SQL服务器2014年档案显示我的第二个请求不会被发送到SQL Server。为什么呢?


解决方案

我觉得这是你的 p.Name 投影内那不处理你的第二个查询。



随着RC1的,EF7还不知道怎么做左外连接。总之,他们都知道,这是得到正确的一个非常重要的事情,并且他们做这个工作。



据报道,的问题3186 GitHub上,还有一些开发者的评论就可以了。



< 。p>我评论自己与另一瑞普有点像你



合作者maumar的言论:




问题是,LINQ的(对象)LOJ的概念并不自身存在



提出的解决方法是使用
的SelectMany-群组加入-DefaultIfEmpty组合代表了选装的导航,然后折叠此
模式进入LOJ在我们的关系的管道。问题是,这种
创建更复杂的查询(主要由于引入
子查询的),目前打破为多数的非平凡的情况。
那些虫子需要,才能够修复与
导航属性扩充的问题得到解决。



当然,我们承认这是一个高优先级的bug,因为它可能返回
不正确的结果。



I'm trying to perform a left outer join request with EF7 (7.0.0-rc1-final), vNext RC1 (rc1-final) and SQL Server 2014

Database :

Pet: Id, Name

User: Id, Name, #PetId

This one works:

var queryWorks = from u in _context.Users
                 join p in _context.Pets on u.PetId equals p.Id into pp
                 from p in pp.DefaultIfEmpty()
                 select new {
                     UserName = u.Name,
                     Pet = p
                 };

but this one doesn't work (Message = "Sequence contains no elements"):

var queryFails = from u in _context.Users
                 join p in _context.Pets on u.PetId equals p.Id into pp
                 from p in pp.DefaultIfEmpty()
                 select new {
                     UserName = u.Name,
                     PetName = (p == null ? "NULL" : p.Name)
                 };

SQL Server Profile 2014 shows me that the second request is not sent to the SQL Server. Why ?

解决方案

I think it's your p.Name inside the projection of your second query that is not handled.

As of RC1, EF7 does not yet know how to do left outer joins. In short, they are aware that it's a really important thing to get right, and they are working on it.

It was reported in issue 3186 on github, and some of the devs commented on it.

I commented myself with another repro a little like yours.

Collaborator "maumar" remarks:

Problem is that in Linq (to objects) the concept of LOJ doesn't exist on its own.

Proposed fix is to represent optional navigation using SelectMany-GroupJoin-DefaultIfEmpty combination and then collapse this pattern into LOJ in our relational pipeline. Problem is that this creates much more complex queries (mainly due to introduction of subqueries) and currently breaks for majority of non-trivial cases. Those bugs need to be addressed before we can fix the problem with navigation property expansion.

We do recognize this as a high priority bug, as it potentially returns incorrect results.

这篇关于左外连接实体框架的核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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