Search code examples
c#entity-frameworkentity-framework-4entity-framework-6

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. We ran into an issue where EF5 and EF6 generate the same IQueryable in different ways which have different results.

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

In EF5, the SQL looks like this (simplified a bit):

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

And in EF6, the SQL looks like:

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. 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?


Solution

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

    this post, NULL value handling in Entity Framework, should be of some help.

    "DbContext.ContextOptions.UseCSharpNullComparisonBehavior switch, which will automatically embed NULL comparison logic into your Entity Framework queries when the values are nullable"

    "In EF5, the UseCSharpNullComparisonBehavior flag defaults to false. EF6.0 introduced a breaking change where the flag defaults to true "