Skip to content

Support JSON complex properties nested in table-split complex properties - #39834

Open
jeremy-morren wants to merge 3 commits into
dotnet:mainfrom
jeremy-morren:complex-json-in-table-split
Open

jeremy-morren wants to merge 3 commits into
dotnet:mainfrom
jeremy-morren:complex-json-in-table-split

Conversation

@jeremy-morren

@jeremy-morren jeremy-morren commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

A complex property mapped to columns (table splitting) can now contain complex properties and collections mapped to JSON columns in the same table:

modelBuilder.Entity<Order>()
    .ComplexProperty(o => o.Shipping, sb =>
    {
        sb.ComplexProperty(s => s.Address).ToJson();
        sb.ComplexCollection(s => s.Parcels).ToJson();
    });

// Columns: Id, Shipping_Carrier, Shipping_Address (JSON), Shipping_Parcels (JSON)
  • 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.
  • ExecuteUpdate can 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:

  • Querying an abstract TPC base type with a JSON complex property threw UnreachableException (Querying a TPC base type with a JSON complex property throws #39814).
  • For an optional complex property mapped to columns, a required property inside an optional nested complex property was used to decide whether it's null, so e.Outer == null returned extra rows and a populated Outer could be materialized as null.
  • Validation rejected an optional complex property whose only required property is in a nested required complex property, even though that column identifies it.
  • With TPT, a JSON complex property declared on a base type was reported as mapped to the tables of the derived types (GetMappedStoreObjects), which index validation relies on.
  • The default mapping used by FromSql ignored a configured column name for a property of a complex type, unlike for a property of an entity type.
  • An explicit JSON column name used by another column wasn't validated and failed at runtime; it's now a validation error, in views too. Two JSON complex properties mapped to the same view column weren't validated either.

Intentionally out of scope:

  • RelationalStrings.NestedComplexPropertyJsonWithTableSharing is removed rather than kept for compatibility: it's the error for the mapping this PR supports, and no provider uses it.
  • Column name collisions are case-sensitive, like EF's existing column collision handling, even though e.g. SQLite, and SQL Server with the default collation, compare column names case-insensitively.
  • When two columns trade names, e.g. a property takes the default name of a JSON column that is then renamed to the property's old name, the generated renames fail on the database, as for any two columns that trade names.
  • With the SQL Server json type, ExecuteUpdate of a property inside a nested JSON column fails for rows where the column is NULL, as for any nullable JSON column.
  • Setting a whole table-split complex property that contains JSON complex properties with ExecuteUpdate still throws ExecuteUpdateOverJsonIsNotSupported.
  • The rest of Normalize default JSON column name handling #39730 (the default JSON column names of owned entity types), different JSON column names per table or view (Json: allow mapping json structure to a different column on view and on table #28584, Allow specifying different JSON column facets per table in TPT, TPC or entity splitting #38214), and name clashes in function and ToSqlQuery mappings.

AI did all the heavy lifting for this PR.

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>
@jeremy-morren
jeremy-morren requested a review from a team as a code owner November 27, 2026 01:01
Copilot AI balanced review requested due to automatic review settings November 27, 2026 01:01
@jeremy-morren

Copy link
Copy Markdown
Contributor Author

This pr includes the commit on #39815 so that PR can be closed if required.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment on lines +103 to +104
else if (TryGetNestedComplexPropertyValue(structuralObject, complexProperty, out var value)
&& value == null)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jeremy-morren
jeremy-morren force-pushed the complex-json-in-table-split branch from 658f84a to efb4811 Compare November 27, 2026 01:34
Copilot AI balanced review requested due to automatic review settings November 27, 2026 01:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

jeremy-morren and others added 2 commits November 27, 2026 17:42
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>
@jeremy-morren
jeremy-morren force-pushed the complex-json-in-table-split branch from efb4811 to 1301170 Compare November 27, 2026 06:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

2 participants