Skip to content

Fix complex type JSON mapping and compiled model bugs - #39448

Merged
AndriySvyryd merged 4 commits into
dotnet:mainfrom
jeremy-morren:fix-complex-json-mapping
Oct 30, 2026
Merged

AndriySvyryd merged 4 commits into
dotnet:mainfrom
jeremy-morren:fix-complex-json-mapping

Conversation

@jeremy-morren

@jeremy-morren jeremy-morren commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Fix complex type JSON mapping and compiled model bugs

Fixes 4 bugs found while investigating complex/owned type JSON mappings and limitations.

Added 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_nullable
  • TableSplittingTestBase: 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_column
  • ComplexTypeQueryRelationalTestBase: Json_complex_properties_on_sibling_types_mapped_to_same_column, Json_complex_properties_on_base_and_derived_types_mapped_to_same_column_with_TPT
  • RelationalModelValidatorTest: the new errors, collisions between owned and complex types on the same, base/derived (TPH, TPT, TPC), sibling and table-splitting types, and no OptionalDependentWithoutIdentifyingPropertyWarning for a dependent with a required JSON complex property or collection

Copilot AI balanced review requested due to automatic review settings October 29, 2026 02:49
@jeremy-morren
jeremy-morren requested a review from a team as a code owner October 29, 2026 02:49

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

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

@jeremy-morren
jeremy-morren force-pushed the fix-complex-json-mapping branch from d8ee083 to 24c6761 Compare October 29, 2026 13:24
Copilot AI balanced review requested due to automatic review settings October 29, 2026 13:24

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

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

@jeremy-morren
jeremy-morren force-pushed the fix-complex-json-mapping branch from 24c6761 to 308727b Compare October 29, 2026 20:27
Copilot AI balanced review requested due to automatic review settings October 29, 2026 20:27

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

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

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

@jeremy-morren
jeremy-morren force-pushed the fix-complex-json-mapping branch from f8c8989 to 1c27359 Compare October 30, 2026 00:04
Copilot AI balanced review requested due to automatic review settings October 30, 2026 00:04

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.

🔵 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
Copilot AI balanced review requested due to automatic review settings October 30, 2026 14:37
@jeremy-morren
jeremy-morren force-pushed the fix-complex-json-mapping branch from 1c27359 to 4c8c882 Compare October 30, 2026 14:37

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

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 AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit f6a2140 into dotnet:main Oct 30, 2026
18 of 19 checks passed
@github-actions github-actions Bot added the api-review This PR or issue is introducing public API changes that need to be reviewed label Oct 30, 2026
@github-actions

Copy link
Copy Markdown
Contributor

API review baseline changes for src/EFCore.Relational/EFCore.Relational.baseline.json

Show diff

The diff below was generated by ApiChief between the base and the PR.

  static class Microsoft.EntityFrameworkCore.Diagnostics.RelationalStrings
+ static string ComplexCollectionSharedJsonColumn(object? complexCollection, object? complexProperty, object? column, object? table);
+ static string ComplexPropertiesSharedJsonColumn(object? complexProperty1, object? complexProperty2, object? column, object? table);
+ static string NestedComplexPropertyJsonColumn(object? complexProperty, object? column, object? containingType, object? containingColumn);
  class Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator : Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator
- virtual void ValidateJsonTable(IReadOnlyList<IEntityType> mappedTypes, in StoreObjectIdentifier table, IDiagnosticsLogger<Validation> logger);
+ virtual void ValidateJsonColumns(IReadOnlyList<IEntityType> mappedTypes, in StoreObjectIdentifier table, IDiagnosticsLogger<Validation> logger);
+ virtual void ValidateJsonOwnedEntityType(IEntityType entityType, in StoreObjectIdentifier table, IDiagnosticsLogger<Validation> logger);

@jeremy-morren
jeremy-morren deleted the fix-complex-json-mapping branch October 30, 2026 21:00
@jeremy-morren

Copy link
Copy Markdown
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api-review This PR or issue is introducing public API changes that need to be reviewed community-contribution

Projects

None yet

3 participants