dotnet / dotnet/efcore

Query optimization compares case sensitively while SqlServer is case insensitive?

Open
#34,862 2 comments 0 reactions 1 assignee Claimed by @AndriySvyryd View on GitHub
area-query consider-for-next-release customer-reported regression
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

I just updated from EF 7.0.10 on .NET6 -> 8.0.8 on .NET8 and noticed a small, yet undocumented breaking change.
We use Sql Server and I have not tested this with other providers.

What happen is if you have a query that includes multiple string equality checks with AND:
```C#
Context.Things.Where(thing => thing.Name.Equals("TheName") && thing.Name.Equals("tHeNaMe"));
```
Notice that the to clauses are the same string if compared case insensitive. I am aware that this query seems weird, but when the query is dynamically built from user input, this can happen.

However, this worked fine with EF 7 and produced SQL query like
```SQL
...[e].[Name] = N'TheName' AND [e].[Name] = N'tHeNaMe'
```
Since SQL Server treats the string comparisons case insensitevely this query will return all 'Things' that have a name like "TheName", no matter the casing.

But in EF 8 the same query produces this SQL query:
```SQL
...WHERE 0 = 1
```
which obviously doesn't return anything.

If we remove one of the 'Equals' clauses or make both clauses have the same casing, it still works.
It seems to me like EF8 is looking at the query, comparing the two 'Equals' clauses with case sensitive comparing and concluding that this is contradictory and just makes SQL query to terminate quickly.

Is this change intended? If so, it should be documented as breaking change.

EF Core version: 8.0.8
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Windows 11
IDE: Rider 2024.2

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.