相同的linq查询在不同的项目中生成不同的sql [英] same linq query generate different sql in different projects

查看:62
本文介绍了相同的linq查询在不同的项目中生成不同的sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查询是:

        var query =
                from stokHareket1 in ce.StokHarekets
                from stokHareket2 in ce.StokHarekets
                join stok in ce.Stoks on stokHareket1.StokId equals stok.Id
                where (
                stokHareket1.StokId == stokHareket2.StokId
                )
                select new
                {
                    result = stokHareket1.StokId
                };

在第一个项目EF版本中是: 5.0.0 ,在第二个项目中是 6.1.3

In first project EF version is: 5.0.0 and in the second one: 6.1.3

它提供了第一个项目:

SELECT 
[Extent1].[StokId] AS [StokId]
FROM  [dbo].[StokHareket] AS [Extent1]
INNER JOIN [dbo].[StokHareket] AS [Extent2] ON [Extent1].[StokId] = [Extent2].[StokId]
WHERE [Extent1].[StokId] IS NOT NULL

第二个:

SELECT 
[Extent1].[StokId] AS [StokId]
FROM  [dbo].[StokHareket] AS [Extent1]
INNER JOIN [dbo].[StokHareket] AS [Extent2] ON ([Extent1].[StokId] = [Extent2].[StokId])   
/*THIS LINE*/OR (([Extent1].[StokId] IS NULL) AND ([Extent2].[StokId] IS NULL)) 
WHERE [Extent1].[StokId] IS NOT NULL

/*THIS LINE*/不同

造成这种差异的原因是什么?

What could be reason for this difference?

为了更好地显示图像,以下是屏幕截图:

To better visualize, here is screen shots :

在db中,stokHareket表

In db, stokHareket Table

在Edmx中:

推荐答案

这似乎是EF6中引入的一项更改(可能是更早的版本;我没有确凿的证据). EF6开始将.NET空比较规则(null == null为true)注入SQL查询,除非您将上下文配置的UseDatabaseNullSemantics属性设置为true:

This appears to be a change introduced in EF6 (possibly earlier; I haven't found conclusive evidence). EF6 started injecting .NET null comparison rules (null == null is true) into SQL queries, unless you set the UseDatabaseNullSemantics property on the context's configuration to true:

public class MyContext : DbContext
{
    public MyContext()
    {
        this.Configuration.UseDatabaseNullSemantics = true;
    }
}

无论如何,它不会影响您的结果-WHERE子句会过滤掉StokId为null的所有记录.

In any case, it won't affect your results - the WHERE clause is filtering out any records where StokId is null.

这篇关于相同的linq查询在不同的项目中生成不同的sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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