Repository navigation
Fix complex type JSON mapping and compiled model bugs - #39448
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Table-splitting JSON column collisions can silently discard writes or clear principal data, alongside unresolved complex-collection tracking failures and invalid SQL generation.
4 open issues
1 file not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file
What changed in this PR
This PR addresses four complex-type JSON mapping and compiled-model issues in EF Core’s relational infrastructure. Existing compiled models must be regenerated.
Changes:
- Generate property counts for complex collections in compiled models and update provider baselines.
- Make JSON columns nullable for optional table-sharing dependents and clear them when dependents are removed.
- Validate nested JSON mappings and column collisions while allowing sharing between mutually exclusive sibling types, with regression coverage.
| File | Description |
|---|---|
test/EFCore.SqlServer.FunctionalTests/TableSplittingSqlServerTest.cs |
Adds SQL Server dependent-removal SQL assertions. |
test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/ComplexTypes/PrincipalDerivedEntityType.cs |
Adds generated complex-collection counts. |
test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_collection_change_tracking/TagComplexUnsafeAccessors.cs |
Adds generated element-field accessors. |
test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_collection_change_tracking/EntityWithComplexCollectionUnsafeAccessors.cs |
Adds generated entity-field accessors. |
test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_collection_change_tracking/EntityWithComplexCollectionEntityType.cs |
Adds entity metadata and collection-count baseline. |
test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_collection_change_tracking/DbContextModelBuilder.cs |
Adds SQL Server relational mapping baseline. |
test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_collection_change_tracking/DbContextModel.cs |
Adds compiled-model initialization baseline. |
test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_collection_change_tracking/DbContextAssemblyAttributes.cs |
Registers the generated SQL Server model. |
test/EFCore.SqlServer.FunctionalTests/Query/ComplexTypeQuerySqlServerTest.cs |
Adds sibling JSON-column SQL assertions. |
test/EFCore.Sqlite.FunctionalTests/TableSplittingSqliteTest.cs |
Adds SQLite dependent-removal SQL assertions. |
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_collection_change_tracking/TagComplexUnsafeAccessors.cs |
Adds generated element-field accessors. |
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_collection_change_tracking/EntityWithComplexCollectionUnsafeAccessors.cs |
Adds generated entity-field accessors. |
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_collection_change_tracking/EntityWithComplexCollectionEntityType.cs |
Adds entity metadata and collection-count baseline. |
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_collection_change_tracking/DbContextModelBuilder.cs |
Adds SQLite relational mapping baseline. |
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_collection_change_tracking/DbContextModel.cs |
Adds compiled-model initialization baseline. |
test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_collection_change_tracking/DbContextAssemblyAttributes.cs |
Registers the generated SQLite model. |
test/EFCore.Sqlite.FunctionalTests/Query/ComplexTypeQuerySqliteTest.cs |
Adds sibling JSON-column SQL assertions. |
test/EFCore.Relational.Tests/Infrastructure/RelationalModelValidatorTest.Json.cs |
Covers nested mappings and inheritance-related collisions. |
test/EFCore.Relational.Specification.Tests/TableSplittingTestBase.cs |
Tests optional dependent insertion and removal. |
test/EFCore.Relational.Specification.Tests/Scaffolding/CompiledModelRelationalTestBase.cs |
Tests collection tracking with compiled models. |
test/EFCore.Relational.Specification.Tests/Query/ComplexTypeQueryRelationalTestBase.cs |
Tests querying and updating shared sibling JSON columns. |
test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/ComplexTypes/PrincipalDerivedEntityType.cs |
Updates generated collection counts. |
test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/ComplexTypes/PrincipalDerivedEntityType.cs |
Updates generated collection counts. |
src/EFCore.Relational/Update/ModificationCommand.cs |
Clears JSON columns for removed shared-row dependents. |
src/EFCore.Relational/Properties/RelationalStrings.resx |
Adds JSON-mapping validation messages. |
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs |
Adds generated validation-message accessors. |
src/EFCore.Relational/Metadata/Internal/RelationalModel.cs |
Accounts for optional dependents in JSON-column nullability. |
src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs |
Centralizes JSON-column collision checks and rejects nested mappings. |
src/EFCore.Relational/Extensions/RelationalPropertyExtensions.cs |
Makes optional-dependent detection available internally. |
src/EFCore.Relational/EFCore.Relational.baseline.json |
Records new diagnostic APIs. |
src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs |
Emits complex-collection property counts. |
Review effort: Balanced
d8ee083 to
24c6761
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The validator incorrectly rejects safe JSON column sharing across mutually exclusive TPH branches because root resolution ignores non-owned table-splitting principal relationships.
2 open issues
3 resolved since last review
1 file not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file
Review effort: Balanced
24c6761 to
308727b
Compare
There was a problem hiding this comment.
🟡 Changes recommended
A critical update-generation bug can emit duplicate JSON-column assignments and fail on SQL Server, while model validation also rejects valid sibling mappings in a table-splitting hierarchy.
2 open issues
2 resolved since last review
1 file not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file
Review effort: Balanced
308727b to
f8c8989
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Critical replacement scenarios can leave stale JSON stored or throw during SaveChanges, while the JSON existence check adds redundant document-sized allocations.
3 open issues
2 resolved since last review
1 file not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file
Review effort: Balanced
f8c8989 to
1c27359
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
Cross-cutting changes to JSON materialization, shared-row updates, and NativeAOT snapshots require provider and generated-code runtime validation that could not be performed in this review environment.
No open issues 🎉
3 resolved since last review
1 file not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file
Review effort: Balanced
- Emit the PropertyCounts of each complex collection's complex type in the compiled model, alongside the entity type counts, including complex collections nested in complex collection elements - Fixes a NullReferenceException when tracking an entity with a complex collection using a NativeAOT compiled model - Compiled models need to be regenerated to pick up the fix Fixes dotnet#39444
- Make the JSON container column nullable when the complex property is declared on an optional table-sharing dependent - Set the JSON column to NULL when the dependent is removed while the row is kept for the principal - Resolve JSON columns against the command's table, so TPT base and derived types can use the same JSON column name Fixes dotnet#39445
- Throw NestedComplexPropertyJsonColumn when ToJson() is called on a complex property whose declaring type is already mapped to JSON - Throw ComplexPropertiesSharedJsonColumn when complex properties on types that can share a row are mapped to the same JSON column Fixes dotnet#39446
- Check the JSON columns of owned entity types and complex properties together, per table - Allow complex properties to share a JSON column only on types in different branches of the same hierarchy - Don't count a complex property inherited from a base type once per derived type Fixes dotnet#39447
1c27359 to
4c8c882
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Removing the protected virtual ValidateJsonTable overload breaks source and binary compatibility for provider and custom-validator subclasses, so the existing signature and virtual dispatch must be preserved.
1 open issue
1 file not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file
Review effort: Balanced
AndriySvyryd
left a comment
There was a problem hiding this comment.
Thanks for your contribution!
API review baseline changes for
|
|
Thanks, I was using an agent to work on complex types on owned types, and these were the bugs that came up before I could start on that. |


Fix complex type JSON mapping and compiled model bugs
Fixes 4 bugs found while investigating complex/owned type JSON mappings and limitations.
SetCountsand the snapshot factories for each complex collection's complex type, including complex collections nested in complex collection elements, so they can be tracked without dynamic code (NativeAOT). Compiled models need to be regenerated. Fixes NativeAOT compiled model: NullReferenceException tracking an entity with a complex collection #39444 and Add the *ValuesFactory implementations for complex collections to the compiled model #37750IsOptionalSharingDependent), and set toNULLwhen the dependent is removed, including when it's replaced by another type that doesn't map the column. A required JSON complex property or collection identifies the dependent like a required property, so a dependent with only JSON complex properties is materialized, filtered and compared tonullcorrectly. JSON columns are resolved against the table being saved, so TPT base and derived types can use the same JSON column name. The snapshot doesn't store the JSON column's nullability, so no migration is generated for existing models: databases created before this fix keep the columnNOT NULLuntil it's altered manually. Fixes JSON complex property on an optional table-sharing dependent: column is NOT NULL and not cleared on delete #39445NestedComplexPropertyJsonColumnandComplexPropertiesSharedJsonColumn. The nestedToJson()case is a regression in 10.0.1 ([release/10.0] Fix HasJsonPropertyName for complex properties #37020), and the shared column case silently loses data since Redo query complex type support for TPC and other hierarchy scenarios #37410 (11.0). Fixes Invalid JSON column mappings of complex properties do not throw #39446protected virtualRelationalModelValidator.ValidateSharedJsonColumns. Complex properties may share a column only on types in different branches of the same hierarchy, including sibling table-splitting dependent types. Dependents of sibling principal types still can't, as querying the dependent returns the rows of the other types (Querying a table-splitting dependent of a derived TPH type returns rows of other types #39458); owned JSON entities still can't share a column (Support mapping multiple types to the same JSON column (sharing) #28592). Fixes False JSON column collisions for complex properties when a table has an owned JSON entity #39447Added tests:
CompiledModelRelationalTestBase:Complex_collection_change_tracking,Nested_complex_collection_change_tracking(insert, query, then edit, add, remove and reorder loaded elements)MigrationsModelDifferTest:Making_table_splitting_dependent_with_json_complex_property_optional_makes_json_column_nullableTableSplittingTestBase:Can_remove_optional_dependent_with_json_complex_property,Can_remove_optional_TPT_dependent_with_json_complex_properties,Can_query_optional_dependent_with_only_json_complex_property,Can_query_optional_dependent_with_only_json_complex_collection,Can_replace_dependent_with_sibling_type_sharing_json_column,Can_replace_dependent_with_sibling_type_using_different_json_column,Can_replace_dependent_with_base_type_without_json_columnComplexTypeQueryRelationalTestBase:Json_complex_properties_on_sibling_types_mapped_to_same_column,Json_complex_properties_on_base_and_derived_types_mapped_to_same_column_with_TPTRelationalModelValidatorTest: the new errors, collisions between owned and complex types on the same, base/derived (TPH, TPT, TPC), sibling and table-splitting types, and noOptionalDependentWithoutIdentifyingPropertyWarningfor a dependent with a required JSON complex property or collection