Skip to content

Fix NullReferenceException when accessing null complex properties in TPH with shared columns#37695

Draft
Copilot wants to merge 6 commits intomainfrom
copilot/port-pr-37389-to-main
Draft

Fix NullReferenceException when accessing null complex properties in TPH with shared columns#37695
Copilot wants to merge 6 commits intomainfrom
copilot/port-pr-37389-to-main

Conversation

Copy link
Contributor

Copilot AI commented Feb 13, 2026

Port PR #37389 to main

  • Update PropertyAccessorsFactory.cs to preserve null checks for nullable reference type complex properties in TPH scenarios
  • Add test cases in ComplexTypesTrackingSqlServerTest.cs for TPH shared complex property column scenarios
  • Add model entities for test cases (InheritedItemBase, InheritedItem1, InheritedItem2, SharedPrice)
  • Fix the check to verify if property's declaring type is a complex type (not if property itself is complex)
  • Skip setting properties in null complex types during AcceptChanges
  • Add filter to First() queries to avoid FirstWithoutOrderByAndFilterWarning
  • Only add null checks for nullable complex properties (not required ones) to avoid breaking compiled model tests
  • Run code review and security checks

All changes complete. The fix now correctly handles null complex types in two critical places:

  1. PropertyAccessorsFactory: Preserves null checks when accessing properties declared in nullable complex types (checks ComplexProperty.IsNullable)
  2. InternalEntryBase.AcceptChanges: Skips setting store-generated values on properties inside null complex types

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.

…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
Copilot AI requested a review from AndriySvyryd February 13, 2026 05:01
…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>
…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)
{
Copy link
Member

Choose a reason for hiding this comment

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

@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)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants