1

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?

1 Answer 1

2
> 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 "

Sign up to request clarification or add additional context in comments.

2 Comments

I haven't tried it yet, but it appears to be exactly what I need. From the link you posted, it appears that EF6 default of true is the recommended value due to performance reasons related to SQL query plans choosing to not use indexes due to the way the legacy behavior built the queries. This may be of note for future readers.
We use code first, and is semantically the same but reverse. DbContext.Configuration.UseDatabaseNullSematics = true is equivalent to UseCSharpNullComparisonBehavior = false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.