Skip to content

[release/10.0] Fix comparison to null for split entities#37987

Merged
AndriySvyryd merged 1 commit into
dotnet:release/10.0from
roji:backport/35293-to-10.0
Mar 24, 2026
Merged

[release/10.0] Fix comparison to null for split entities#37987
AndriySvyryd merged 1 commit into
dotnet:release/10.0from
roji:backport/35293-to-10.0

Conversation

@roji
Copy link
Copy Markdown
Member

@roji roji commented Mar 24, 2026

Fixes #35293
Backports #37358

Description
When an entity uses entity splitting (mapped to multiple tables via SplitToTable), comparing it to null in a LINQ query throws InvalidOperationException: Sequence contains more than one element. The root cause is a call to SingleOrDefault() on GetViewOrTableMappings(), which returns multiple mappings for split entities. The fix replaces this with a switch expression that handles both single-table and multi-table (entity splitting) scenarios, selecting the principal table for split entities.

Customer impact
Users who use entity splitting and compare the split entity (or a navigation to it) to null in a LINQ query get an unhandled InvalidOperationException at query time. For example:

context.Set<InvoiceLine>().Select(x => new
{
    x.Id,
    Name = x.Invoice.Creator != null ? x.Invoice.Creator!.FullName : ""
});

A workaround exists: users can compare the key property instead of the entity (e.g. x.Invoice.CreatorId != null), but this is not immediately discoverable.

How found
User reported on EF Core 8.0.7 / 9.0.0. The issue has 2 upvotes and 3 additional comment authors asking for a fix or workaround, indicating multiple users are affected.

Regression
This is not a regression from a previous major version. Entity splitting was introduced in EF Core 7.0, and the bug has been present since then — the code always assumed a single table mapping when checking null comparisons.

Testing
1 new test added (Compare_split_entity_to_null) with SQL baseline verification on SQL Server.

Risk
Very low. The change is limited to a single method that resolves the table for null comparison checks. The fix adds handling for the multi-table case while preserving the existing single-table logic. Quirk added (Microsoft.EntityFrameworkCore.Issue35293) to allow opting out of the fix.

@roji roji requested a review from a team as a code owner March 24, 2026 17:52
Copilot AI review requested due to automatic review settings March 24, 2026 17:52
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Backport addressing EF Core issue #35293 where comparing an entity-splitting entity to null in LINQ could throw due to assuming a single table/view mapping during null-equality rewrite.

Changes:

  • Update null-comparison rewrite logic to handle multiple view/table mappings (entity splitting) and select the principal mapping, with an opt-out AppContext switch.
  • Add a relational specification test covering split entity != null.
  • Add SQL Server SQL baseline for the new test.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.StructuralEquality.cs Adjusts entity-null comparison rewrite to support split entities and adds an AppContext quirk switch.
test/EFCore.Relational.Specification.Tests/Query/EntitySplittingQueryTestBase.cs Adds a new test covering comparing a split entity to null.
test/EFCore.SqlServer.FunctionalTests/Query/EntitySplittingQuerySqlServerTest.cs Adds SQL Server AssertSql baseline for the new test.

Copy link
Copy Markdown
Member

@artl93 artl93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. Customer reported; blocking upgrade.

@AndriySvyryd AndriySvyryd added this to the 10.0.6 milestone Mar 24, 2026
@AndriySvyryd AndriySvyryd merged commit b85b2e3 into dotnet:release/10.0 Mar 24, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants