Fix complex property JSON column not marked nullable in TPH hierarchy#37781
Fix complex property JSON column not marked nullable in TPH hierarchy#37781Copilot wants to merge 5 commits intorelease/10.0from
Conversation
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
… check Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
|
@copilot Update the PR description with a nice summary, referencing the issue being fixed |
Done — updated the PR description with a summary referencing the issue, following the servicing PR template (Description, Customer impact, How found, Regression, Testing, Risk). |
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where complex properties stored as JSON columns were incorrectly marked as non-nullable in Table-Per-Hierarchy (TPH) inheritance scenarios. In TPH mapping, all entity types in the hierarchy share the same database table, so columns exclusive to derived types must be nullable to accommodate rows of other sibling types.
Changes:
- Added null-checking logic in
RelationalModel.CreateContainerColumnto mark JSON columns as nullable when the complex property is declared on a derived entity type using TPH mapping strategy - Added unit test
Complex_property_json_column_is_nullable_in_TPH_hierarchyto verify the fix
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/EFCore.Relational/Metadata/Internal/RelationalModel.cs | Added TPH-specific nullability check for complex property JSON columns to match owned entity behavior |
| test/EFCore.Relational.Tests/Metadata/RelationalModelTest.cs | Added test with TPH base entity and two derived types (one with, one without complex property) to verify JSON column is nullable |
|
@copilot Also update CompiledModelSqlServerTest.ComplexTypes baseline |
…in TPH hierarchy Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
In a TPH (Table Per Hierarchy) mapping, columns exclusive to a derived entity type must be nullable because rows belonging to sibling derived types will not have a value for those columns. This was correctly handled for regular columns and owned entity JSON columns, but not for complex property JSON columns. As a result, databases would raise a not-null constraint violation when saving an instance of a sibling derived type.
Changes Made
RelationalModel.CreateContainerColumn: When computingIsNullablefor a complex property JSON column, added a check that the complex property's declaring entity type has a base type and is using TPH mapping strategy (nullstrategy defaults to TPH). The check is evaluated before thechain.Any(p => p.IsNullable)check so that TPH-derived types are correctly identified before inspecting the chain.RelationalModelTest: Added unit testComplex_property_json_column_is_nullable_in_TPH_hierarchyverifying that the JSON container column is marked nullable when a complex property is defined on a derived type in a TPH hierarchy.CompiledModelSqlServerTest.ComplexTypesbaseline (DbContextModelBuilder.cs): Updated to reflect that theManyOwnedJSON column (declared on the derivedPrincipalDerivedtype in a TPH hierarchy) is now correctly marked as nullable.Testing
Complex_property_json_column_is_nullable_in_TPH_hierarchyvalidates the fixRelationalModelTesttests continue to passCompiledModelSqlServerTest.ComplexTypesbaseline updated to match the corrected nullabilityOriginal 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.