Repository navigation
Support JSON complex properties nested in table-split complex properties - #39834
jeremy-morren wants to merge 3 commits into
Conversation
Querying an abstract TPC base type with a JSON complex property threw "Could not find JSON container column", since the JSON reader looked the column up in the query mappings of the base type, which isn't mapped to a table. Use the model column referenced by the projected JSON column instead, which the UNION over the concrete tables already provides. Fixes dotnet#39814 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
This pr includes the commit on #39815 so that PR can be closed if required. |
There was a problem hiding this comment.
🟡 Changes recommended
Critical issues leave upgrades without required JSON-column renames and cause database-value refreshes to discard present optional objects when their JSON scalars are null.
2 open issues
1 file not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file
What changed in this PR
This PR extends EF Core’s relational mapping support so complex properties mapped to table columns can contain JSON-mapped properties and collections. It addresses #36558 and the abstract TPC query failure in #39814, while partially addressing JSON column naming in #39730.
Changes:
- Adds nested JSON materialization, change tracking, updates, and null handling.
- Prefixes and uniquifies default JSON column names, preserves column identity during migration comparisons, and validates collisions.
- Expands shared regression coverage, SQL Server and SQLite SQL assertions, and compiled-model baselines.
| File | Description |
|---|---|
| test/EFCore.SqlServer.FunctionalTests/Update/ComplexCollectionJsonUpdateSqlServerTest.cs | Adds nested JSON update SQL assertions. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/ComplexTypes/DbContextModelBuilder.cs | Reflects configured scalar column names. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/OwnedWithComplexEntityType.cs | Adds compiled metadata for nested Landmark. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/OwnedWithComplex0EntityType.cs | Adds compiled nested JSON Landmark mapping. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/LocationComplexUnsafeAccessors.cs | Adds Landmark field access. |
| test/EFCore.SqlServer.FunctionalTests/Query/ComplexTypeQuerySqlServerTest.cs | Adds nested JSON and TPC regression baselines. |
| test/EFCore.SqlServer.FunctionalTests/Query/Associations/ComplexTableSplittingNestedJson/ComplexTableSplittingNestedJsonSqlServerFixture.cs | Configures SQL Server nested JSON tests. |
| test/EFCore.SqlServer.FunctionalTests/Query/Associations/ComplexTableSplittingNestedJson/ComplexTableSplittingNestedJsonMiscellaneousSqlServerTest.cs | Covers filtering, equality, raw SQL, and joins. |
| test/EFCore.SqlServer.FunctionalTests/Query/Associations/ComplexTableSplittingNestedJson/ComplexTableSplittingNestedJsonCollectionSqlServerTest.cs | Adds collection SQL baselines and provider limitations. |
| test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs | Updates prefixed JSON column expectations. |
| test/EFCore.Sqlite.FunctionalTests/Update/ComplexCollectionJsonUpdateSqliteTest.cs | Adds nested JSON update SQL assertions. |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/OwnedWithComplexEntityType.cs | Adds compiled metadata for nested Landmark. |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/OwnedWithComplex0EntityType.cs | Adds compiled nested JSON Landmark mapping. |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/LocationComplexUnsafeAccessors.cs | Adds Landmark field access. |
| test/EFCore.Sqlite.FunctionalTests/Query/ComplexTypeQuerySqliteTest.cs | Adds nested JSON and TPC regression baselines. |
| test/EFCore.Sqlite.FunctionalTests/Query/Associations/ComplexTableSplittingNestedJson/ComplexTableSplittingNestedJsonSqliteFixture.cs | Configures SQLite nested JSON tests. |
| test/EFCore.Sqlite.FunctionalTests/Query/Associations/ComplexTableSplittingNestedJson/ComplexTableSplittingNestedJsonProjectionSqliteTest.cs | Covers projections and unsupported APPLY queries. |
| test/EFCore.Sqlite.FunctionalTests/Query/Associations/ComplexTableSplittingNestedJson/ComplexTableSplittingNestedJsonMiscellaneousSqliteTest.cs | Adds raw SQL, equality, and join baselines. |
| test/EFCore.Sqlite.FunctionalTests/Query/Associations/ComplexTableSplittingNestedJson/ComplexTableSplittingNestedJsonCollectionSqliteTest.cs | Runs shared collection tests on SQLite. |
| test/EFCore.Sqlite.FunctionalTests/Migrations/MigrationsSqliteTest.cs | Updates prefixed JSON column expectations. |
| test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs | Tests JSON column identity during renames. |
| test/EFCore.Relational.Tests/Infrastructure/RelationalModelValidatorTest.Json.cs | Tests nesting, collisions, inheritance, and nullability. |
| test/EFCore.Relational.Specification.Tests/Update/ComplexCollectionJsonUpdateTestBase.cs | Adds shared nested JSON persistence tests. |
| test/EFCore.Relational.Specification.Tests/Scaffolding/CompiledModelRelationalTestBase.cs | Exercises compiled nested JSON mappings. |
| test/EFCore.Relational.Specification.Tests/Query/Associations/ComplexTableSplittingNestedJson/ComplexTableSplittingNestedJsonRelationalFixtureBase.cs | Defines shared mixed column/JSON models. |
| test/EFCore.Relational.Specification.Tests/Query/Associations/ComplexTableSplittingNestedJson/ComplexTableSplittingNestedJsonProjectionRelationalTestBase.cs | Adds identity-resolution projection tests. |
| test/EFCore.Relational.Specification.Tests/Query/Associations/ComplexTableSplittingNestedJson/ComplexTableSplittingNestedJsonMiscellaneousRelationalTestBase.cs | Adds shared raw SQL, equality, and join tests. |
| test/EFCore.Relational.Specification.Tests/Query/Associations/ComplexTableSplittingNestedJson/ComplexTableSplittingNestedJsonCollectionRelationalTestBase.cs | Reuses collection tests with JSON limitations. |
| test/EFCore.Relational.Specification.Tests/Migrations/MigrationsTestBase.cs | Updates shared JSON column naming assertions. |
| src/EFCore/Query/Internal/StructuralTypeMaterializerSource.cs | Refines nested complex-property null detection. |
| src/EFCore/ChangeTracking/Internal/ChangeDetector.cs | Marks nested collections on null transitions. |
| src/EFCore/ChangeTracking/Internal/ArrayPropertyValues.cs | Restores present complex objects during value copying. |
| src/EFCore.Relational/Update/ModificationCommand.cs | Handles nested JSON writes and ancestor null transitions. |
| src/EFCore.Relational/Query/RelationalSqlTranslatingExpressionVisitor.StructuralEquality.cs | Extends nested JSON equality and null comparisons. |
| src/EFCore.Relational/Query/RelationalShapedQueryCompilingExpressionVisitor.ShaperProcessingExpressionVisitor.cs | Materializes nested JSON and resolves TPC columns. |
| src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.cs | Supports traversal through table-split complex properties. |
| src/EFCore.Relational/Query/Internal/StructuralTypeProjectionExpressionExtensions.cs | Binds JSON columns through containing complex properties. |
| src/EFCore.Relational/Query/Internal/RelationalStructuralTypeMaterializerSource.cs | Separates query JSON shaping from value-buffer materialization. |
| src/EFCore.Relational/Properties/RelationalStrings.resx | Adds collision diagnostics and removes the nesting restriction message. |
| src/EFCore.Relational/Properties/RelationalStrings.Designer.cs | Updates generated diagnostic accessors. |
| src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs | Matches JSON columns by complex-property path first. |
| src/EFCore.Relational/Metadata/Internal/TableBase.cs | Uses default-mapping JSON column names. |
| src/EFCore.Relational/Metadata/Internal/RelationalPropertyExtensions.cs | Accounts for nullable containing complex properties. |
| src/EFCore.Relational/Metadata/Internal/RelationalModel.cs | Aligns default mappings with JSON and configured scalar names. |
| src/EFCore.Relational/Metadata/Internal/RelationalEntityTypeExtensions.cs | Includes nested JSON in dependent existence checks. |
| src/EFCore.Relational/Metadata/Conventions/SharedTableConvention.cs | Uniquifies default JSON names across tables and views. |
| src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs | Allows nesting and validates existence markers and collisions. |
| src/EFCore.Relational/Extensions/RelationalTypeBaseExtensions.cs | Resolves prefixed nested JSON column names. |
| src/EFCore.Relational/Extensions/RelationalPropertyExtensions.cs | Shares complex-property column-prefix helpers. |
| src/EFCore.Relational/Extensions/RelationalComplexPropertyExtensions.cs | Corrects inherited JSON store-object mappings. |
| src/EFCore.Relational/Extensions/RelationalComplexPropertyBuilderExtensions.cs | Documents nested JSON naming behavior. |
| src/EFCore.Relational/Extensions/RelationalComplexCollectionBuilderExtensions.cs | Documents nested JSON collection naming behavior. |
| src/EFCore.Relational/EFCore.Relational.baseline.json | Updates the diagnostic API baseline. |
Review effort: Balanced
| else if (TryGetNestedComplexPropertyValue(structuralObject, complexProperty, out var value) | ||
| && value == null) |
There was a problem hiding this comment.
Valid, thanks. Fixed by narrowing the validation instead: an optional complex property mapped to columns now needs a required scalar property on a path of required complex properties (which can lead into a nested JSON complex property), as before this PR. A required JSON complex property alone no longer identifies it, since property values, including the database values of an entry, can't tell a JSON document with only null values from a missing one. So GetDatabaseValues(), Reload() and ToObject() derive the null flags from that required property. Covered by Throws_for_optional_complex_property_with_only_optional_properties_in_nested_json_complex_property.
658f84a to
efb4811
Compare
There was a problem hiding this comment.
🟡 Changes recommended
An optional Annex sibling can cause GetDatabaseValues().ToObject() and Reload() to clear a populated Outer, requiring path-aware required-scalar filtering and synchronous/asynchronous refresh regression coverage.
2 open issues
1 resolved since last review
1 file not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file
Review effort: Balanced
| && complexProperty.IsNullable | ||
| && complexProperty.ComplexType.GetProperties().All(m => m.IsNullable)) | ||
| && complexProperty.ComplexType.GetProperties().All(m => m.IsNullable) | ||
| && !HasRequiredNestedProperty(complexProperty.ComplexType)) |
There was a problem hiding this comment.
Valid, thanks. When choosing the required property that shows whether an optional complex property is null, ArrayPropertyValues took the first required property anywhere in it, including one inside an optional nested complex property. It now only uses one on a path of required complex properties, the same rule the materializer and validation use, shared in one helper (FindIdentifyingProperty). Covered by Database_values_of_optional_complex_property_identified_by_nested_json_complex_property_with_null_optional_sibling, which checks CurrentValues.ToObject(), GetDatabaseValues()/GetDatabaseValuesAsync() and Reload()/ReloadAsync() for this shape.
Fixes dotnet#36558 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The default name of a JSON complex property nested in a table-split complex property can be the name of another column in the table or view. Such names are made unique, and an explicit JSON column name used by another column is a validation error. Partially addresses dotnet#39730 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
efb4811 to
1301170
Compare


A complex property mapped to columns (table splitting) can now contain complex properties and collections mapped to JSON columns in the same table:
ToJson()without a name prefixes the column with the containing complex properties (Shipping_Address), like their other columns, and is made unique if another column already has that name, also when the other column is added later: migrations match JSON columns by their complex property, so the JSON column is renamed rather than its data being left in the new column.ExecuteUpdatecan set a nested JSON property or a property inside it.Fixes #36558. Fixes #39814. Partially addresses #39730 (column name uniquification for complex types mapped to JSON).
Existing bugs fixed along the way:
UnreachableException(Querying a TPC base type with a JSON complex property throws #39814).e.Outer == nullreturned extra rows and a populatedOutercould be materialized asnull.GetMappedStoreObjects), which index validation relies on.FromSqlignored a configured column name for a property of a complex type, unlike for a property of an entity type.Intentionally out of scope:
RelationalStrings.NestedComplexPropertyJsonWithTableSharingis removed rather than kept for compatibility: it's the error for the mapping this PR supports, and no provider uses it.jsontype,ExecuteUpdateof a property inside a nested JSON column fails for rows where the column isNULL, as for any nullable JSON column.ExecuteUpdatestill throwsExecuteUpdateOverJsonIsNotSupported.ToSqlQuerymappings.AI did all the heavy lifting for this PR.