简体   繁体   English

实体框架的版本之间生成的查询不同

[英]Generated Query is different between versions of Entity Framework

I am in the process of upgrading Entity Framework and have successfully upgraded from v5 (but since we are on .NET 4.0, the assembly actually says v4.4) to the latest release of v6. 我正在升级Entity Framework,并且已成功地从v5升级(但是由于我们使用的是.NET 4.0,因此程序集实际上说的是v4.4)到v6的最新版本。 We ran into an issue where EF5 and EF6 generate the same IQueryable in different ways which have different results. 我们遇到一个问题,其中EF5和EF6以不同的方式生成相同的IQueryable并产生不同的结果。

The LINQ query is: Context.MyTable.SingleOrDefault(x => x.StringProperty != ""); LINQ查询为: Context.MyTable.SingleOrDefault(x => x.StringProperty != "");

In EF5, the SQL looks like this (simplified a bit): 在EF5中,SQL看起来像这样(简化了一点):

SELECT TOP (2) ID AS ID, StringProperty AS StringProperty FROM MyTable WHERE N'' <> StringProperty

And in EF6, the SQL looks like: 在EF6中,SQL看起来像:

SELECT TOP (2) ID AS ID, StringProperty AS StringProperty FROM MyTable WHERE NOT((N'' = StringProperty) AND (StringProperty IS NOT NULL))

If StringProperty is null, the EF5 query does not return the row, while the EF6 version does. 如果StringProperty为null,则EF5查询不返回该行,而EF6版本则返回。 I have not seen this documented anywhere (not entirely sure where to look). 我没有在任何地方看到此文档(不完全确定从何处看)。 This example is trivial to fix, though difficult to find. 尽管很难找到此示例,但修复起来很简单。 Is there a way to turn on the legacy behavior? 有没有办法打开传统行为?

More importantly, I'd like to know if there are any other changes like this where the query is generated a different way that would result in different query results. 更重要的是,我想知道是否还有其他类似的变化,其中查询是通过不同的方式生成的,从而导致不同的查询结果。 Is there a list of breaking changes in EF6? EF6中是否有重大更改的清单?

> Is there a way to turn on the legacy behavior?

this post, NULL value handling in Entity Framework , should be of some help. 这篇文章, 实体框架中的NULL值处理 ,应该有所帮助。

" DbContext.ContextOptions.UseCSharpNullComparisonBehavior switch, which will automatically embed NULL comparison logic into your Entity Framework queries when the values are nullable" DbContext.ContextOptions.UseCSharpNullComparisonBehavior开关,当值可为空时,它将自动将NULL比较逻辑嵌入到您的实体框架查询中”

"In EF5, the UseCSharpNullComparisonBehavior flag defaults to false. EF6.0 introduced a breaking change where the flag defaults to true " “在EF5中,UseCSharpNullComparisonBehavior标志默认为false。EF6.0引入了一项重大更改,其中标志默认为true”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM