Fix NullReferenceException when accessing null complex properties in TPH with shared columns#37695
Draft
Fix NullReferenceException when accessing null complex properties in TPH with shared columns#37695
Conversation
…es in TPH Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Port changes from PR #37389 to main branch
Fix NullReferenceException when accessing null complex properties in TPH with shared columns
Feb 13, 2026
test/EFCore.SqlServer.FunctionalTests/ComplexTypesTrackingSqlServerTest.cs
Show resolved
Hide resolved
…tself is complex The NRE was occurring during SaveChanges when setting properties inside a null complex type. The original fix incorrectly checked if the property itself was a complex property, but it should check if the property is declared in a complex type. When accessing Amount inside SharedPrice, Amount is a scalar property but its declaring type (SharedPrice) is a complex type. Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
The NRE was occurring in AcceptChanges when trying to set store-generated values on properties inside null complex types. Added check to skip properties declared in complex types when the complex type instance is null, following the same pattern used elsewhere in the codebase (lines 1612-1613, 1627-1628). Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
test/EFCore.SqlServer.FunctionalTests/ComplexTypesTrackingSqlServerTest.cs
Outdated
Show resolved
Hide resolved
…rning Added .Where(i => i.Name == "Item1") filter before FirstAsync() calls to avoid the warning about using First without OrderBy and filter operators. This makes the tests more deterministic and follows EF Core testing best practices. Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
| @@ -8,13 +8,102 @@ public class ComplexTypesTrackingSqlServerTest( | |||
| ITestOutputHelper testOutputHelper) | |||
| : ComplexTypesTrackingSqlServerTestBase<ComplexTypesTrackingSqlServerTest.SqlServerFixture>(fixture, testOutputHelper) | |||
| { | |||
Member
There was a problem hiding this comment.
@copilot Microsoft.EntityFrameworkCore.Scaffolding.CompiledModelSqlServerTest.ComplexTypes and other implementations of the ComplexTypes test are failing with
System.AggregateException : Differences found in 2 files (Difference found in PrincipalBaseEntityType.cs) (Difference found in PrincipalDerivedEntityType.cs)\r\n---- System.Exception : Difference found in PrincipalBaseEntityType.cs\r\n-------- Assert.Equal() Failure: Strings differ\r\n ↓ (pos 76408)\r\nExpected: ···"edType instance) => instance.Details,\r\n "···\r\nActual: ···"edType instance) => (instance == null ? d"···\r\n ↑ (pos 76408)\r\n---- System.Exception : Difference found in PrincipalDerivedEntityType.cs\r\n-------- Assert.Equal() Failure: Strings differ\r\n ↓ (pos 9682)\r\nExpected: ···"edType instance) => instance.Details,\r\n "···\r\nActual: ···"edType instance) => (instance == null ? d"···\r\n ↑ (pos 9682)
Contributor
Author
There was a problem hiding this comment.
Fixed in 4e16643. Added check for ComplexProperty.IsNullable so null checks are only generated for nullable complex properties, not required ones. This prevents unnecessary null checks in compiled models while still protecting against NRE for nullable complex properties in TPH scenarios.
Modified PropertyAccessorsFactory to only add null checks when the complex property is actually nullable (IsNullable=true). This prevents adding unnecessary null checks in compiled models for required complex properties while still protecting against NRE for nullable complex properties in TPH scenarios with shared columns. Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
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.
Port PR #37389 to main
All changes complete. The fix now correctly handles null complex types in two critical places:
ComplexProperty.IsNullable)The fix is optimized to only add null checks where actually needed (nullable complex properties), keeping compiled models clean and efficient while protecting against NRE in TPH scenarios with shared columns.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.