Fix complex property JSON column not marked nullable in TPH hierarchy#37781
Draft
Copilot wants to merge 2 commits intorelease/10.0from
Draft
Fix complex property JSON column not marked nullable in TPH hierarchy#37781Copilot wants to merge 2 commits intorelease/10.0from
Copilot wants to merge 2 commits intorelease/10.0from
Conversation
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix complex property nullability in TPH hierarchy
Fix complex property JSON column not marked nullable in TPH hierarchy
Feb 24, 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.
In a TPH hierarchy, a JSON column backed by a complex property on a derived type was incorrectly created as
NOT NULL, causing constraint violations when inserting rows for sibling types that don't have that property.This was a parity gap vs. owned entity JSON columns, which already handle this via a
BaseType != nullcheck.Changes
RelationalModel.CreateContainerColumn: In the complex property branch, after computing nullability from the property/chain, additionally check ifchain[0].DeclaringType(the outermost declaring entity type) has aBaseType. If so, forceIsNullable = true— matching the existing owned entity logic.RelationalModelTest: AddedComplex_property_json_column_is_nullable_in_TPH_hierarchyunit test with a 3-type TPH hierarchy (abstract base + two derived types, one with aComplexProperty(...).ToJson()) asserting the resulting JSON column is nullable.Example
Original prompt
This section details on the original issue you should resolve
<issue_title>Complex property stored as json will be marked non-nullable even in TPH class hierarchy</issue_title>
<issue_description>### Bug description
When two subclasses are stored in the same table using TPH, all columns mapped from properties that are exclusive to one of the subclasses will be marked as nullable, becuase if the row is an instance of the 'other' subclass that column will not have a value. Except for complex properties stored as json. This leads to exceptions when storing data because the database expect a value where there is none. This issue does not exist when using owned entities instead of complex entities.
Workaround is to explicitly specifiy
IsRequired(false)but that doesn't seem the intended solution.Your code
Stack traces
Verbose output
EF Core version
10.0.1
Database provider
Npgsql.EntityFrameworkCore.PostgreSQL
Target framework
.NET 10.0
Operating system
MacOS
IDE
Rider</issue_description>
Comments on the Issue (you are @copilot in this section)
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.