Skip to content

Query optimization compares case sensitively while SqlServer is case insensitive? #34862

@kopfsick

Description

@kopfsick

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:

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

...[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:

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No fields configured for Bug.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions