[release/10.0] Fix comparison to null for split entities#37987
Merged
Conversation
There was a problem hiding this comment.
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. |
AndriySvyryd
approved these changes
Mar 24, 2026
artl93
approved these changes
Mar 24, 2026
Member
artl93
left a comment
There was a problem hiding this comment.
Approved. Customer reported; blocking upgrade.
This was referenced Mar 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #35293
Backports #37358
Description
When an entity uses entity splitting (mapped to multiple tables via
SplitToTable), comparing it tonullin a LINQ query throwsInvalidOperationException: Sequence contains more than one element. The root cause is a call toSingleOrDefault()onGetViewOrTableMappings(), 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
nullin a LINQ query get an unhandledInvalidOperationExceptionat query time. For example: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.