-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Cosmos: Complex properties model building support for specifying property names #37919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AndriySvyryd
merged 10 commits into
dotnet:main
from
JoasE:feature/complex-properties-model-building
Mar 17, 2026
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8bbaf04
Add model building extensions
JoasE 0646625
Fix typo
JoasE 8a2ee7a
Apply suggestions from code review
JoasE bc09d17
Merge branch 'feature/complex-properties-model-building' of https://g…
JoasE 7fe3c70
Don't allow empty property names for complex properties
JoasE 796b6b2
Skip non-persisted properties during serialization in query
JoasE 9ccce42
Update docs
JoasE 9f8e02e
Cleanup
JoasE 5ee20aa
Update CosmosApiConsistencyFixture
JoasE da8a70f
Remove comment
JoasE File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/EFCore.Cosmos/Extensions/CosmosComplexCollectionBuilderExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore; | ||
|
|
||
| /// <summary> | ||
| /// Cosmos-specific extension methods for <see cref="ComplexCollectionBuilder" />. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. | ||
| /// </remarks> | ||
| public static class CosmosComplexCollectionBuilderExtensions | ||
JoasE marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| /// <summary> | ||
| /// Configures the property name that the complex collection is mapped to when stored as an embedded document. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. | ||
| /// </remarks> | ||
| /// <param name="complexPropertyBuilder">The builder for the complex type being configured.</param> | ||
| /// <param name="name">The name of the parent property.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static ComplexCollectionBuilder ToJsonProperty( | ||
| this ComplexCollectionBuilder complexPropertyBuilder, | ||
| string? name) | ||
| { | ||
| complexPropertyBuilder.Metadata.SetJsonPropertyName(name); | ||
| return complexPropertyBuilder; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configures the property name that the the complex collection is mapped to when stored as an embedded document. | ||
JoasE marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. | ||
| /// </remarks> | ||
| /// <param name="complexPropertyBuilder">The builder for the complex type being configured.</param> | ||
| /// <param name="name">The name of the parent property.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static ComplexCollectionBuilder<TComplex> ToJsonProperty<TComplex>( | ||
| this ComplexCollectionBuilder<TComplex> complexPropertyBuilder, | ||
| string? name) | ||
| where TComplex : notnull | ||
| { | ||
| complexPropertyBuilder.Metadata.SetJsonPropertyName(name); | ||
| return complexPropertyBuilder; | ||
| } | ||
| } | ||
60 changes: 60 additions & 0 deletions
60
src/EFCore.Cosmos/Extensions/CosmosComplexCollectionTypePropertyBuilderExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| // ReSharper disable once CheckNamespace | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore; | ||
|
|
||
| /// <summary> | ||
| /// Cosmos-specific extension methods for <see cref="ComplexCollectionTypePropertyBuilder" />. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. | ||
| /// </remarks> | ||
| public static class CosmosComplexCollectionTypePropertyBuilderExtensions | ||
| { | ||
| /// <summary> | ||
| /// Configures the property name that the property is mapped to when targeting Azure Cosmos. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// If an empty string is supplied, the property will not be persisted. | ||
| /// </para> | ||
| /// <para> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. | ||
| /// </para> | ||
| /// </remarks> | ||
| /// <param name="propertyBuilder">The builder for the property being configured.</param> | ||
| /// <param name="name">The name of the property.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static ComplexCollectionTypePropertyBuilder ToJsonProperty( | ||
| this ComplexCollectionTypePropertyBuilder propertyBuilder, | ||
| string name) | ||
| { | ||
| Check.NotEmpty(name); | ||
|
|
||
JoasE marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| propertyBuilder.Metadata.SetJsonPropertyName(name); | ||
|
|
||
| return propertyBuilder; | ||
JoasE marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configures the property name that the property is mapped to when targeting Azure Cosmos. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. | ||
| /// </remarks> | ||
| /// <typeparam name="TProperty">The type of the property being configured.</typeparam> | ||
| /// <param name="propertyBuilder">The builder for the property being configured.</param> | ||
| /// <param name="name">The name of the property.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static ComplexCollectionTypePropertyBuilder<TProperty> ToJsonProperty<TProperty>( | ||
| this ComplexCollectionTypePropertyBuilder<TProperty> propertyBuilder, | ||
| string name) | ||
| => (ComplexCollectionTypePropertyBuilder<TProperty>)ToJsonProperty((ComplexCollectionTypePropertyBuilder)propertyBuilder, name); | ||
|
|
||
| // Vector and fulltext properties are not supported on collection types | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
src/EFCore.Cosmos/Extensions/CosmosComplexPropertyBuilderExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore; | ||
|
|
||
| /// <summary> | ||
| /// Cosmos-specific extension methods for <see cref="ComplexPropertyBuilder" />. | ||
| /// </summary> | ||
JoasE marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. | ||
| /// </remarks> | ||
| public static class CosmosComplexPropertyBuilderExtensions | ||
| { | ||
| /// <summary> | ||
| /// Configures the property name that the complex property is mapped to when stored as an embedded document. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. | ||
| /// </remarks> | ||
| /// <param name="complexPropertyBuilder">The builder for the complex type being configured.</param> | ||
| /// <param name="name">The name of the parent property.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static ComplexPropertyBuilder ToJsonProperty( | ||
| this ComplexPropertyBuilder complexPropertyBuilder, | ||
| string? name) | ||
| { | ||
| complexPropertyBuilder.Metadata.SetJsonPropertyName(name); | ||
| return complexPropertyBuilder; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configures the property name that the complex property is mapped to when stored as an embedded document. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. | ||
| /// </remarks> | ||
| /// <param name="complexPropertyBuilder">The builder for the complex type being configured.</param> | ||
| /// <param name="name">The name of the parent property.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static ComplexPropertyBuilder<TComplex> ToJsonProperty<TComplex>( | ||
| this ComplexPropertyBuilder<TComplex> complexPropertyBuilder, | ||
| string? name) | ||
| where TComplex : notnull | ||
| { | ||
| complexPropertyBuilder.Metadata.SetJsonPropertyName(name); | ||
| return complexPropertyBuilder; | ||
| } | ||
| } | ||
35 changes: 35 additions & 0 deletions
35
src/EFCore.Cosmos/Extensions/CosmosComplexPropertyExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal; | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore; | ||
|
|
||
| /// <summary> | ||
| /// Complex property extension methods for Cosmos metadata. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. | ||
| /// </remarks> | ||
| public static class CosmosComplexPropertyExtensions | ||
| { | ||
| /// <summary> | ||
| /// Returns the property name that the property is mapped to when targeting Cosmos. | ||
| /// </summary> | ||
| /// <param name="property">The property.</param> | ||
| /// <returns>Returns the property name that the property is mapped to when targeting Cosmos.</returns> | ||
| public static string GetJsonPropertyName(this IReadOnlyComplexProperty property) | ||
| => (string?)property[CosmosAnnotationNames.PropertyName] | ||
| ?? property.Name; | ||
|
|
||
| /// <summary> | ||
| /// Sets the property name that the property is mapped to when targeting Cosmos. | ||
| /// </summary> | ||
| /// <param name="property">The property.</param> | ||
| /// <param name="name">The name to set.</param> | ||
| public static void SetJsonPropertyName(this IMutableComplexProperty property, string? name) | ||
| => property.SetOrRemoveAnnotation( | ||
| CosmosAnnotationNames.PropertyName, | ||
| Check.NullButNotEmpty(name)); | ||
| } |
56 changes: 56 additions & 0 deletions
56
src/EFCore.Cosmos/Extensions/CosmosComplexTypePrimitiveCollectionBuilderExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore; | ||
|
|
||
| /// <summary> | ||
| /// Cosmos-specific extension methods for <see cref="ComplexTypePrimitiveCollectionBuilder" />. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. | ||
| /// </remarks> | ||
| public static class CosmosComplexTypePrimitiveCollectionBuilderExtensions | ||
| { | ||
| /// <summary> | ||
| /// Configures the property name that the property is mapped to when targeting Azure Cosmos. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// If an empty string is supplied, the property will not be persisted. | ||
| /// </para> | ||
| /// <para> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. | ||
| /// </para> | ||
| /// </remarks> | ||
| /// <param name="primitiveCollectionBuilder">The builder for the property being configured.</param> | ||
| /// <param name="name">The name of the property.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static ComplexTypePrimitiveCollectionBuilder ToJsonProperty( | ||
| this ComplexTypePrimitiveCollectionBuilder primitiveCollectionBuilder, | ||
| string name) | ||
| { | ||
| Check.NotNull(name); | ||
|
|
||
| primitiveCollectionBuilder.Metadata.SetJsonPropertyName(name); | ||
|
|
||
| return primitiveCollectionBuilder; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configures the property name that the property is mapped to when targeting Azure Cosmos. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. | ||
| /// </remarks> | ||
| /// <typeparam name="TProperty">The type of the property being configured.</typeparam> | ||
| /// <param name="primitiveCollectionBuilder">The builder for the property being configured.</param> | ||
| /// <param name="name">The name of the property.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static ComplexTypePrimitiveCollectionBuilder<TProperty> ToJsonProperty<TProperty>( | ||
| this ComplexTypePrimitiveCollectionBuilder<TProperty> primitiveCollectionBuilder, | ||
| string name) | ||
| => (ComplexTypePrimitiveCollectionBuilder<TProperty>)ToJsonProperty((ComplexTypePrimitiveCollectionBuilder)primitiveCollectionBuilder, name); | ||
| } |
60 changes: 60 additions & 0 deletions
60
src/EFCore.Cosmos/Extensions/CosmosComplexTypePropertyBuilderExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| // ReSharper disable once CheckNamespace | ||
|
|
||
| namespace Microsoft.EntityFrameworkCore; | ||
|
|
||
| /// <summary> | ||
| /// Cosmos-specific extension methods for <see cref="ComplexTypePropertyBuilder" />. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. | ||
| /// </remarks> | ||
| public static class CosmosComplexTypePropertyBuilderExtensions | ||
| { | ||
| /// <summary> | ||
| /// Configures the property name that the property is mapped to when targeting Azure Cosmos. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <para> | ||
| /// If an empty string is supplied, the property will not be persisted. | ||
| /// </para> | ||
| /// <para> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. | ||
| /// </para> | ||
| /// </remarks> | ||
| /// <param name="propertyBuilder">The builder for the property being configured.</param> | ||
| /// <param name="name">The name of the property.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static ComplexTypePropertyBuilder ToJsonProperty( | ||
| this ComplexTypePropertyBuilder propertyBuilder, | ||
| string name) | ||
| { | ||
| Check.NotNull(name); | ||
|
|
||
| propertyBuilder.Metadata.SetJsonPropertyName(name); | ||
|
|
||
| return propertyBuilder; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Configures the property name that the property is mapped to when targeting Azure Cosmos. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and | ||
| /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. | ||
| /// </remarks> | ||
| /// <typeparam name="TProperty">The type of the property being configured.</typeparam> | ||
| /// <param name="propertyBuilder">The builder for the property being configured.</param> | ||
| /// <param name="name">The name of the property.</param> | ||
| /// <returns>The same builder instance so that multiple calls can be chained.</returns> | ||
| public static ComplexTypePropertyBuilder<TProperty> ToJsonProperty<TProperty>( | ||
| this ComplexTypePropertyBuilder<TProperty> propertyBuilder, | ||
| string name) | ||
| => (ComplexTypePropertyBuilder<TProperty>)ToJsonProperty((ComplexTypePropertyBuilder)propertyBuilder, name); | ||
|
|
||
| // Vector and fulltext properties are not supported on complex types, because you can't define indexes on complex types | ||
AndriySvyryd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.