Fix NullReferenceException when accessing null complex properties in TPH with shared columns #37389
+147
−1
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.
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 callingentry.OriginalValues.ToObject().Changes
Modified
PropertyAccessorsFactory.CreateMemberAccessPreviously disabled null checks unconditionally when
fromDeclaringTypewas true. Now preserves null checks for nullable reference type complex properties:addNullCheckparameterAdded test coverage in
TPHComplexPropertySharingTestOriginal prompt
This section details on the original issue you should resolve
<issue_title>map two classes with same nullable complex properties to same column will result in a NullReferenceException</issue_title>
<issue_description>### Bug description
I have a base class and several child classes with some of them share some complex properties. I try to map them into one table and want to map same complex properties into the same column. This will result in a NullReferenceException if the property is null. See example code.
With different column names the same setup works.
Your code
Stack traces
Verbose output
EF Core version
10.0.1
Database provider
No response
Target framework
.NET 10
Operating system
No response
IDE
Rider</issue_description>
<agent_instructions>Start by creating a test that reproduces the reported issue</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@roji Confirmed, see exception below. @AndriySvyryd is this scenario supposed to be currently supported?