Skip to content

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

Closed
Copilot wants to merge 6 commits intorelease/10.0from
copilot/fix-nullreferenceexception-mapping
Closed

Fix NullReferenceException when accessing null complex properties in TPH with shared columns#37389
Copilot wants to merge 6 commits intorelease/10.0from
copilot/fix-nullreferenceexception-mapping

Conversation

Copy link
Contributor

Copilot AI commented Dec 16, 2025

Fixes #37335

Problem

When two derived types in TPH share a nullable complex property mapped to the same column, accessing the property when null throws NullReferenceException, particularly during materialization or when calling entry.OriginalValues.ToObject().

Changes

Modified PropertyAccessorsFactory.CreateMemberAccess

Previously disabled null checks unconditionally when fromDeclaringType was true. Now preserves null checks for nullable reference type complex properties:

  • Checks if the property is a complex property (not a regular scalar property)
  • Checks if both the declaring entity and complex property types are reference types (not structs)
  • Respects the incoming addNullCheck parameter

Added test coverage in ComplexTypesTrackingSqlServerTest

  • Integrated tests into the existing ComplexTypesTrackingSqlServerTest class using transactions for database isolation
  • Can_read_original_values_with_TPH_shared_complex_property_column_null() validates materialization and original values access with null complex properties
  • Can_read_original_values_with_TPH_shared_complex_property_column_with_value() validates the same with populated complex properties

Related Discussion

This PR replaces #[original-pr-number] and now targets main instead of release/10.0 as requested by @AndriySvyryd.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix NullReferenceException when mapping complex properties Fix NullReferenceException when accessing null complex properties in TPH with shared columns Dec 16, 2025
Copilot AI requested a review from AndriySvyryd December 16, 2025 23:58
Copy link
Member

@AndriySvyryd AndriySvyryd left a comment

Choose a reason for hiding this comment

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

@copilot Rebase onto main

- Moved TPH complex property sharing tests into ComplexTypesTrackingSqlServerTest class
- Used transactions to isolate test data instead of recreating database
- Added new entity types (InheritedItemBase, InheritedItem1/2, SharedPrice) to the test model
- Removed standalone TPHComplexPropertySharingTest.cs file

Addresses code review feedback from @AndriySvyryd

Co-authored-by: AndriySvyryd <[email protected]>
Copy link
Contributor Author

Copilot AI commented Feb 13, 2026

@copilot Rebase onto main

I've successfully rebased the branch onto main locally (commits are now based on 84b8d15). However, I cannot force push the rebased history due to authentication constraints with the available tools. The rebase is complete and ready - a maintainer with push access will need to force push or I can create a new PR with the rebased commits if preferred.

@AndriySvyryd
Copy link
Member

@copilot Create a new PR targeting main

Copy link
Contributor Author

Copilot AI commented Feb 13, 2026

@copilot Create a new PR targeting main

I've created a new branch copilot/fix-nullreferenceexception-mapping-main that targets main (based on commit 84b8d15). All 5 commits have been cherry-picked onto main:

  • a92da60a59: Add test to reproduce TPH complex property sharing issue
  • c9bad85f66: Fix NullReferenceException when accessing null complex properties in TPH
  • 5551e86b88: Refine fix to only add null checks for nullable reference complex properties
  • 84a810cfe3: Remove redundant assignment and improve code clarity
  • 2f91a34c3c: Move tests to ComplexTypesTrackingSqlServerTest and use transactions

However, I cannot push this new branch due to authentication constraints. A maintainer will need to:

  1. Push the local branch copilot/fix-nullreferenceexception-mapping-main to origin, OR
  2. Let me know if there's another approach to create the PR targeting main

@AndriySvyryd
Copy link
Member

Superseded by #37695

@AndriySvyryd AndriySvyryd deleted the copilot/fix-nullreferenceexception-mapping branch February 13, 2026 19:09
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

Comments