Repository navigation
Support complex properties on owned types - #39522
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved JSON nullability defects can incorrectly exclude unmatched outer-join rows or trigger Debug assertions, while default-name handling can overwrite explicit container-column configuration.
3 open issues
1 file not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file
What changed in this PR
Adds complex-property and complex-collection support to owned types, addressing #33170’s migration snapshot compilation failure and extending support across model building, queries, updates, and compiled models.
Changes:
- Adds owned-navigation configuration APIs and preserves complex metadata when owned types are rebuilt.
- Extends relational query binding, JSON column naming, update handling, and validation.
- Adds shared regression tests, provider SQL assertions, migration coverage, and compiled-model baselines.
| File | Description |
|---|---|
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/TagComplexUnsafeAccessors.cs | Generated tag field accessor. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/OwnerWithComplexUnsafeAccessors.cs | Generated owner field accessors. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/OwnerWithComplexEntityType.cs | Compiled owner metadata for SQL Server. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/OwnedWithComplexUnsafeAccessors.cs | Generated owned-type field accessors. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/LocationComplexUnsafeAccessors.cs | Generated location field accessors. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/DbContextModelBuilder.cs | Compiled relational and JSON mappings. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/DbContextModel.cs | Compiled-model initialization baseline. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/DbContextAssemblyAttributes.cs | Registers the SQL Server compiled model. |
| test/EFCore.SqlServer.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/CoordinatesComplexUnsafeAccessors.cs | Generated coordinate field accessors. |
| test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs | SQL Server table-creation assertions. |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/TagComplexUnsafeAccessors.cs | Generated tag field accessor. |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/OwnerWithComplexUnsafeAccessors.cs | Generated owner field accessors. |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/OwnerWithComplexEntityType.cs | Compiled owner metadata for SQLite. |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/OwnedWithComplexUnsafeAccessors.cs | Generated owned-type field accessors. |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/LocationComplexUnsafeAccessors.cs | Generated location field accessors. |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/DbContextModelBuilder.cs | Compiled relational and JSON mappings. |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/DbContextModel.cs | Compiled-model initialization baseline. |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/DbContextAssemblyAttributes.cs | Registers the SQLite compiled model. |
| test/EFCore.Sqlite.FunctionalTests/Scaffolding/Baselines/Complex_properties_on_owned_types/CoordinatesComplexUnsafeAccessors.cs | Generated coordinate field accessors. |
| test/EFCore.Sqlite.FunctionalTests/Migrations/MigrationsSqliteTest.cs | SQLite table-creation assertions. |
| test/EFCore.Specification.Tests/Query/OwnedComplexPropertiesQueryTestBase.cs | Shared query, update, and conversion tests. |
| test/EFCore.Specification.Tests/ModelBuilding/ModelBuilderTest.TestModel.cs | Models for owned complex-type tests. |
| test/EFCore.Specification.Tests/ModelBuilding/ModelBuilderTest.OwnedTypes.cs | Fluent and attribute-based configuration tests. |
| test/EFCore.Specification.Tests/ModelBuilding/ModelBuilderTest.NonGeneric.cs | Non-generic test-builder adapters. |
| test/EFCore.Specification.Tests/ModelBuilding/ModelBuilderTest.Generic.cs | Generic test-builder adapters. |
| test/EFCore.Specification.Tests/ModelBuilding/ModelBuilderTest.cs | Shared complex configuration test contracts. |
| test/EFCore.Specification.Tests/ModelBuilding/ModelBuilderTest.ComplexCollections.cs | Complex collections on owned-type tests. |
| test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs | Migration diffs for owned complex properties. |
| test/EFCore.Relational.Tests/Infrastructure/RelationalModelValidatorTest.Json.cs | Supported and rejected JSON mapping tests. |
| test/EFCore.Relational.Specification.Tests/Scaffolding/CompiledModelRelationalTestBase.cs | Compiled-model mapping and round-trip tests. |
| test/EFCore.Relational.Specification.Tests/Query/OwnedComplexPropertiesQueryRelationalTestBase.cs | Relational mapping, query, and JSON update tests. |
| test/EFCore.Relational.Specification.Tests/ModelBuilding/RelationalModelBuilderTest.cs | Default JSON column naming tests. |
| test/EFCore.Relational.Specification.Tests/Migrations/MigrationsTestBase.cs | Shared migration table-creation test. |
| test/EFCore.Design.Tests/Migrations/Design/CSharpMigrationsGeneratorTest.ModelSnapshot.cs | Snapshot generation and round-trip coverage. |
| test/EFCore.Cosmos.FunctionalTests/Query/OwnedComplexPropertiesQueryCosmosTest.cs | Cosmos query baselines and converter limitation. |
| src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs | Detaches complex properties with owned metadata. |
| src/EFCore/Metadata/Internal/EntityType.cs | Reattaches saved complex properties. |
| src/EFCore/Metadata/Internal/ComplexPropertySnapshot.cs | Regenerates default names during reattachment. |
| src/EFCore/Metadata/Internal/ComplexProperty.cs | Centralizes default complex-type naming. |
| src/EFCore/Metadata/Builders/OwnedNavigationBuilder.cs | Adds complex-property and collection APIs. |
| src/EFCore/EFCore.baseline.json | Records new owned-navigation APIs. |
| src/EFCore.Relational/Update/ModificationCommand.cs | Includes complex paths and guards JSON scalar patches. |
| src/EFCore.Relational/Query/SqlExpressions/SelectExpression.cs | Binds and lifts owned complex projections. |
| src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.ExecuteUpdate.cs | Rejects updates inside JSON-owned complex members. |
| src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.CreateSelect.cs | Shares complex-property mapping helpers. |
| src/EFCore.Relational/Query/JsonQueryExpression.cs | Allows complex bindings on JSON-owned entities. |
| src/EFCore.Relational/Properties/RelationalStrings.resx | Adds the unsupported JSON collection diagnostic. |
| src/EFCore.Relational/Properties/RelationalStrings.Designer.cs | Generated accessor for the new diagnostic. |
| src/EFCore.Relational/Metadata/Conventions/RelationalMapToJsonConvention.cs | Resolves default JSON names at finalization. |
| src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs | Rejects complex properties within owned JSON collections. |
| src/EFCore.Relational/Extensions/RelationalPropertyExtensions.cs | Shares ownership-prefix calculation. |
| src/EFCore.Relational/Extensions/RelationalComplexPropertyBuilderExtensions.cs | Marks default JSON names for deferred prefixing. |
| src/EFCore.Relational/Extensions/RelationalComplexCollectionBuilderExtensions.cs | Applies deferred naming to complex collections. |
| src/EFCore.Relational/EFCore.Relational.baseline.json | Records the new diagnostic API. |
Review effort: Balanced
7b644a8 to
564a135
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Active issues can corrupt configured JSON scalar representations on SQLite and overwrite an explicitly configured container name, causing the model to target the wrong database column.
2 open issues
3 resolved since last review
1 file not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file
Review effort: Balanced
564a135 to
debc26d
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Shared-type ownership transitions can lose or misbind complex-property keys, and SQLite scalar patches bypass configured JSON serialization, risking incorrect values or failures on subsequent loads.
3 open issues
1 file not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file
Review effort: Balanced
debc26d to
6c062bc
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Unresolved complex-property path handling defects can break relationship reattachment, misconfigure nested owned members, and cause queries involving complex-property primary keys to throw.
6 open issues
Shared-type reattachment drops complex-property ownership keys Resolve complex key columns when building owned identifiers · New Resolve dotted paths in complex-type-name overloads · New Rebind complex-member foreign keys on reattachment · New Scalar patches bypass the configured JSON value writer Resetting the container name leaves the default-name marker set
1 file not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file
Review effort: Balanced
6c062bc to
6983f0c
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Relationship reattachment still misresolves complex principal-key paths during a nested-ownership shared-type transition, causing PropertyWrongEntityClrType and requiring a fix with regression coverage.
3 open issues
4 resolved since last review
1 file not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file
Review effort: Balanced
6983f0c to
5af17d9
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
The finalizer can overwrite an explicitly configured JSON container-column name with the ownership-prefixed default, silently changing migration and query targets; the marker must be cleared and both complex-property and complex-collection builder paths covered.
3 open issues
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.
Also add some tests for precompiled queries.
RelationalStructuralTypeShaperExpression.MakeNullable now also marks the JsonQueryExpression of a JSON complex type as nullable, so scalars bound from it over the optional side of a LEFT JOIN get IS NULL compensation and unmatched rows are no longer dropped.
|
Updated as per feedback.
Queries with a key declared on a complex type This applies to any entity type, owned or not:
Set operations over a type with a JSON complex property
Fixed along the way:
Gaps:
As before, AI did most of heavy lifting. |
There was a problem hiding this comment.
🟡 Changes recommended
Explicit JSON column names matching property names are incorrectly prefixed for table-sharing owned navigations, causing queries to target the wrong column.
3 open issues
1 resolved since last review
1 file not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file
Review effort: Balanced
55adf2f to
417940b
Compare
- Compute the ownership prefix in GetContainerColumnName(StoreObjectIdentifier) instead of RelationalMapToJsonConvention - Store an empty container column name for ToJson() without a name, so explicit names are never prefixed - Add Table/View.FindColumn(IComplexProperty)
- Add an OwnedComplexProperties suite under Query/Associations - Move the remaining tests to existing test classes - Add precompiled query tests
…et operations - Bind key properties through the complex property chain in Distinct, set operations and owned navigation expansion - Access the key through its complex properties in entity equality and Contains - Keep non-collection JSON complex properties as references over set operations - Don't use a key on a complex property as the ownership foreign key - Patch scalars in non-null nullable complex properties of JSON owned references
417940b to
9fe8752
Compare
|
1 small note: this PR includes the commit and fix for #39524. Would you like me to close that PR, which will become irrelevant when this PR is merged? |
Yes |
There was a problem hiding this comment.
🟡 Changes recommended
ExecuteUpdate can target a nonexistent view-named JSON column when an owned reference uses different table and view prefixes; the setter and affected JSON references need rebinding to the target table column, with regression coverage.
3 open issues
1 resolved since last review
1 file not reviewed
src/EFCore.Relational/Properties/RelationalStrings.Designer.cs: Generated file
Review effort: Balanced
There was a problem hiding this comment.
🔵 Needs a closer look
Cross-provider changes to persisted JSON paths, metadata reattachment, and query materialization require compatibility review and provider-backed functional and generated-code validation unavailable in this static review.
No open issues 🎉
3 resolved since last review
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
|
|
No problem, thanks for your help reviewing it. I have a DB context with a huge number of owned types that could be complex types, so I'm keen to start using this when available. |


Fixes #33170.
ComplexProperty/ComplexCollectiononOwnedNavigationBuilderandOwnedNavigationBuilder<,>. Snapshots withb1.ComplexProperty(...)(e.g. from[ComplexType]) now compile.ToJson()without a name prefixes the JSON column with the ownership navigations when the owned type shares its owner's table or view (e.g.Owned_Address), like the columns of its other properties. The prefix is computed per table or view inGetContainerColumnName(StoreObjectIdentifier), so it doesn't depend on the order ofToJson()andToTable().ToJson()stores an empty container column name, whichGetContainerColumnNameresolves to the default (explicit names are used as-is). Snapshots emitToJson()for the default.OwnsMany().ToJson(), which is deprecated (#37289);OwnsOne().ToJson()is supported.Distinct, set operations, entity equality with a parameter andContainsover a local collection now work when the key is declared on a complex type. A non-collection JSON complex property stays a reference over set operations, so it can be filtered and projected.ExecuteUpdateon these members throws the existing not-supported error.Based on #39524 (includes that commit). Fixes #39523.
Tests:
[Owned]+[ComplexType], and default and explicit JSON column names for either order ofToJson()andToTable()Query/Associations/OwnedComplexPropertiessuite (SQL Server, SQLite, Cosmos): owned associates with complex nested associatesInheritanceComplexTypesQueryTestBase: TPH/TPT/TPCEntitySplittingQueryTestBaseCustomConvertersTestBase: value converters and comparersNonSharedModelBulkUpdatesTestBase:ExecuteUpdateOwnedEntityQueryRelationalTestBase: table mappings, JSON (includingJsonValueReaderWriter) and SaveChangesComplexTypeQueryRelationalTestBase: keys on complex typesRelationalModelValidatorTest.JsonKnown limitations:
OwnedComplexPropertiessuiteNB: AI did most of the heavily lifting for this PR.