Skip to content

Commit 803eb28

Browse files
[main] Source code updates from dotnet/efcore (#5555)
[main] Source code updates from dotnet/efcore
1 parent c7efe30 commit 803eb28

19 files changed

+1012
-15
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.EntityFrameworkCore;
5+
6+
/// <summary>
7+
/// Cosmos-specific extension methods for <see cref="ComplexCollectionBuilder" />.
8+
/// </summary>
9+
/// <remarks>
10+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
11+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
12+
/// </remarks>
13+
public static class CosmosComplexCollectionBuilderExtensions
14+
{
15+
/// <summary>
16+
/// Configures the property name that the complex collection is mapped to when stored as an embedded document.
17+
/// </summary>
18+
/// <remarks>
19+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
20+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
21+
/// </remarks>
22+
/// <param name="complexPropertyBuilder">The builder for the complex type being configured.</param>
23+
/// <param name="name">The name of the parent property.</param>
24+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
25+
public static ComplexCollectionBuilder ToJsonProperty(
26+
this ComplexCollectionBuilder complexPropertyBuilder,
27+
string? name)
28+
{
29+
complexPropertyBuilder.Metadata.SetJsonPropertyName(name);
30+
return complexPropertyBuilder;
31+
}
32+
33+
/// <summary>
34+
/// Configures the property name that the complex collection is mapped to when stored as an embedded document.
35+
/// </summary>
36+
/// <remarks>
37+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
38+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
39+
/// </remarks>
40+
/// <param name="complexPropertyBuilder">The builder for the complex type being configured.</param>
41+
/// <param name="name">The name of the parent property.</param>
42+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
43+
public static ComplexCollectionBuilder<TComplex> ToJsonProperty<TComplex>(
44+
this ComplexCollectionBuilder<TComplex> complexPropertyBuilder,
45+
string? name)
46+
where TComplex : notnull
47+
{
48+
complexPropertyBuilder.Metadata.SetJsonPropertyName(name);
49+
return complexPropertyBuilder;
50+
}
51+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
// ReSharper disable once CheckNamespace
5+
6+
namespace Microsoft.EntityFrameworkCore;
7+
8+
/// <summary>
9+
/// Cosmos-specific extension methods for <see cref="ComplexCollectionTypePropertyBuilder" />.
10+
/// </summary>
11+
/// <remarks>
12+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
13+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
14+
/// </remarks>
15+
public static class CosmosComplexCollectionTypePropertyBuilderExtensions
16+
{
17+
/// <summary>
18+
/// Configures the property name that the property is mapped to when targeting Azure Cosmos.
19+
/// </summary>
20+
/// <remarks>
21+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
22+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
23+
/// </remarks>
24+
/// <param name="propertyBuilder">The builder for the property being configured.</param>
25+
/// <param name="name">The name of the property.</param>
26+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
27+
public static ComplexCollectionTypePropertyBuilder ToJsonProperty(
28+
this ComplexCollectionTypePropertyBuilder propertyBuilder,
29+
string name)
30+
{
31+
Check.NotEmpty(name);
32+
33+
propertyBuilder.Metadata.SetJsonPropertyName(name);
34+
35+
return propertyBuilder;
36+
}
37+
38+
/// <summary>
39+
/// Configures the property name that the property is mapped to when targeting Azure Cosmos.
40+
/// </summary>
41+
/// <remarks>
42+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
43+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
44+
/// </remarks>
45+
/// <typeparam name="TProperty">The type of the property being configured.</typeparam>
46+
/// <param name="propertyBuilder">The builder for the property being configured.</param>
47+
/// <param name="name">The name of the property.</param>
48+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
49+
public static ComplexCollectionTypePropertyBuilder<TProperty> ToJsonProperty<TProperty>(
50+
this ComplexCollectionTypePropertyBuilder<TProperty> propertyBuilder,
51+
string name)
52+
=> (ComplexCollectionTypePropertyBuilder<TProperty>)ToJsonProperty((ComplexCollectionTypePropertyBuilder)propertyBuilder, name);
53+
54+
// Vector and fulltext properties are not supported on collection types
55+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.EntityFrameworkCore;
5+
6+
/// <summary>
7+
/// Cosmos-specific extension methods for <see cref="ComplexPropertyBuilder" />.
8+
/// </summary>
9+
/// <remarks>
10+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
11+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
12+
/// </remarks>
13+
public static class CosmosComplexPropertyBuilderExtensions
14+
{
15+
/// <summary>
16+
/// Configures the property name that the complex property is mapped to when stored as an embedded document.
17+
/// </summary>
18+
/// <remarks>
19+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
20+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
21+
/// </remarks>
22+
/// <param name="complexPropertyBuilder">The builder for the complex type being configured.</param>
23+
/// <param name="name">The name of the parent property.</param>
24+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
25+
public static ComplexPropertyBuilder ToJsonProperty(
26+
this ComplexPropertyBuilder complexPropertyBuilder,
27+
string? name)
28+
{
29+
complexPropertyBuilder.Metadata.SetJsonPropertyName(name);
30+
return complexPropertyBuilder;
31+
}
32+
33+
/// <summary>
34+
/// Configures the property name that the complex property is mapped to when stored as an embedded document.
35+
/// </summary>
36+
/// <remarks>
37+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
38+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
39+
/// </remarks>
40+
/// <param name="complexPropertyBuilder">The builder for the complex type being configured.</param>
41+
/// <param name="name">The name of the parent property.</param>
42+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
43+
public static ComplexPropertyBuilder<TComplex> ToJsonProperty<TComplex>(
44+
this ComplexPropertyBuilder<TComplex> complexPropertyBuilder,
45+
string? name)
46+
where TComplex : notnull
47+
{
48+
complexPropertyBuilder.Metadata.SetJsonPropertyName(name);
49+
return complexPropertyBuilder;
50+
}
51+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal;
5+
6+
namespace Microsoft.EntityFrameworkCore;
7+
8+
/// <summary>
9+
/// Complex property extension methods for Cosmos metadata.
10+
/// </summary>
11+
/// <remarks>
12+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
13+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
14+
/// </remarks>
15+
public static class CosmosComplexPropertyExtensions
16+
{
17+
/// <summary>
18+
/// Returns the property name that the property is mapped to when targeting Cosmos.
19+
/// </summary>
20+
/// <param name="property">The property.</param>
21+
/// <returns>Returns the property name that the property is mapped to when targeting Cosmos.</returns>
22+
public static string GetJsonPropertyName(this IReadOnlyComplexProperty property)
23+
=> (string?)property[CosmosAnnotationNames.PropertyName]
24+
?? property.Name;
25+
26+
/// <summary>
27+
/// Sets the property name that the property is mapped to when targeting Cosmos.
28+
/// </summary>
29+
/// <param name="property">The property.</param>
30+
/// <param name="name">The name to set.</param>
31+
public static void SetJsonPropertyName(this IMutableComplexProperty property, string? name)
32+
=> property.SetOrRemoveAnnotation(
33+
CosmosAnnotationNames.PropertyName,
34+
Check.NullButNotEmpty(name));
35+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.EntityFrameworkCore;
5+
6+
/// <summary>
7+
/// Cosmos-specific extension methods for <see cref="ComplexTypePrimitiveCollectionBuilder" />.
8+
/// </summary>
9+
/// <remarks>
10+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
11+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
12+
/// </remarks>
13+
public static class CosmosComplexTypePrimitiveCollectionBuilderExtensions
14+
{
15+
/// <summary>
16+
/// Configures the property name that the property is mapped to when targeting Azure Cosmos.
17+
/// </summary>
18+
/// <remarks>
19+
/// <para>
20+
/// If an empty string is supplied, the property will not be persisted.
21+
/// </para>
22+
/// <para>
23+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
24+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
25+
/// </para>
26+
/// </remarks>
27+
/// <param name="primitiveCollectionBuilder">The builder for the property being configured.</param>
28+
/// <param name="name">The name of the property.</param>
29+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
30+
public static ComplexTypePrimitiveCollectionBuilder ToJsonProperty(
31+
this ComplexTypePrimitiveCollectionBuilder primitiveCollectionBuilder,
32+
string name)
33+
{
34+
Check.NotNull(name);
35+
36+
primitiveCollectionBuilder.Metadata.SetJsonPropertyName(name);
37+
38+
return primitiveCollectionBuilder;
39+
}
40+
41+
/// <summary>
42+
/// Configures the property name that the property is mapped to when targeting Azure Cosmos.
43+
/// </summary>
44+
/// <remarks>
45+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
46+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
47+
/// </remarks>
48+
/// <typeparam name="TProperty">The type of the property being configured.</typeparam>
49+
/// <param name="primitiveCollectionBuilder">The builder for the property being configured.</param>
50+
/// <param name="name">The name of the property.</param>
51+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
52+
public static ComplexTypePrimitiveCollectionBuilder<TProperty> ToJsonProperty<TProperty>(
53+
this ComplexTypePrimitiveCollectionBuilder<TProperty> primitiveCollectionBuilder,
54+
string name)
55+
=> (ComplexTypePrimitiveCollectionBuilder<TProperty>)ToJsonProperty((ComplexTypePrimitiveCollectionBuilder)primitiveCollectionBuilder, name);
56+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
// ReSharper disable once CheckNamespace
5+
6+
namespace Microsoft.EntityFrameworkCore;
7+
8+
/// <summary>
9+
/// Cosmos-specific extension methods for <see cref="ComplexTypePropertyBuilder" />.
10+
/// </summary>
11+
/// <remarks>
12+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
13+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
14+
/// </remarks>
15+
public static class CosmosComplexTypePropertyBuilderExtensions
16+
{
17+
/// <summary>
18+
/// Configures the property name that the property is mapped to when targeting Azure Cosmos.
19+
/// </summary>
20+
/// <remarks>
21+
/// <para>
22+
/// If an empty string is supplied, the property will not be persisted.
23+
/// </para>
24+
/// <para>
25+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
26+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
27+
/// </para>
28+
/// </remarks>
29+
/// <param name="propertyBuilder">The builder for the property being configured.</param>
30+
/// <param name="name">The name of the property.</param>
31+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
32+
public static ComplexTypePropertyBuilder ToJsonProperty(
33+
this ComplexTypePropertyBuilder propertyBuilder,
34+
string name)
35+
{
36+
Check.NotNull(name);
37+
38+
propertyBuilder.Metadata.SetJsonPropertyName(name);
39+
40+
return propertyBuilder;
41+
}
42+
43+
/// <summary>
44+
/// Configures the property name that the property is mapped to when targeting Azure Cosmos.
45+
/// </summary>
46+
/// <remarks>
47+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
48+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
49+
/// </remarks>
50+
/// <typeparam name="TProperty">The type of the property being configured.</typeparam>
51+
/// <param name="propertyBuilder">The builder for the property being configured.</param>
52+
/// <param name="name">The name of the property.</param>
53+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
54+
public static ComplexTypePropertyBuilder<TProperty> ToJsonProperty<TProperty>(
55+
this ComplexTypePropertyBuilder<TProperty> propertyBuilder,
56+
string name)
57+
=> (ComplexTypePropertyBuilder<TProperty>)ToJsonProperty((ComplexTypePropertyBuilder)propertyBuilder, name);
58+
}

src/efcore/src/EFCore.Cosmos/Query/Internal/CosmosSerializationUtilities.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public static JToken SerializeObjectToComplexProperty(IComplexType type, object?
5656
{
5757
var jsonPropertyName = property.GetJsonPropertyName();
5858

59+
if (string.IsNullOrEmpty(jsonPropertyName))
60+
{
61+
continue;
62+
}
63+
5964
var propertyValue = property.GetGetter().GetClrValue(value);
6065
#pragma warning disable EF1001 // Internal EF Core API usage.
6166
var providerValue = property.ConvertToProviderValue(propertyValue);
@@ -77,7 +82,7 @@ public static JToken SerializeObjectToComplexProperty(IComplexType type, object?
7782

7883
foreach (var complexProperty in type.GetComplexProperties())
7984
{
80-
var jsonPropertyName = complexProperty.Name;
85+
var jsonPropertyName = complexProperty.GetJsonPropertyName();
8186
var propertyValue = complexProperty.GetGetter().GetClrValue(value);
8287
if (propertyValue is null)
8388
{

src/efcore/src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ private BlockExpression CreateComplexPropertyAssignmentBlock(MemberExpression me
198198
Call(
199199
CosmosProjectionBindingRemovingExpressionVisitorBase.ToObjectWithSerializerMethodInfo.MakeGenericMethod(typeof(JObject)),
200200
Call(_parentJObject, CosmosProjectionBindingRemovingExpressionVisitorBase.GetItemMethodInfo,
201-
Constant(complexProperty.Name))));
201+
Constant(complexProperty.GetJsonPropertyName()))));
202202

203203
var materializeExpression = CreateComplexTypeMaterializeExpression(complexProperty, jObjectVariable);
204204
if (complexProperty.IsNullable)
@@ -227,7 +227,7 @@ private BlockExpression CreateComplexCollectionAssignmentBlock(MemberExpression
227227
Call(
228228
CosmosProjectionBindingRemovingExpressionVisitorBase.ToObjectWithSerializerMethodInfo.MakeGenericMethod(typeof(JArray)),
229229
Call(_parentJObject, CosmosProjectionBindingRemovingExpressionVisitorBase.GetItemMethodInfo,
230-
Constant(complexProperty.Name))));
230+
Constant(complexProperty.GetJsonPropertyName()))));
231231

232232
var jObjectParameter = Parameter(typeof(JObject), "complexJObject" + _currentComplexIndex);
233233
var materializeExpression = CreateComplexTypeMaterializeExpression(complexProperty, jObjectParameter);

src/efcore/src/EFCore.Cosmos/Query/Internal/Expressions/ObjectAccessExpression.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.EntityFrameworkCore.Cosmos.Internal;
5-
using Microsoft.EntityFrameworkCore.Metadata.Internal;
65

76
// ReSharper disable once CheckNamespace
87
namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal;
@@ -42,7 +41,7 @@ public ObjectAccessExpression(
4241
break;
4342
case IComplexProperty complexProperty:
4443
structuralType = complexProperty.ComplexType;
45-
propertyName = complexProperty.Name;
44+
propertyName = complexProperty.GetJsonPropertyName();
4645
break;
4746
default:
4847
throw new UnreachableException($"Unexpected structural property type: {structuralProperty.GetType().FullName}");

src/efcore/src/EFCore.Cosmos/Query/Internal/Expressions/ObjectArrayAccessExpression.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.EntityFrameworkCore.Cosmos.Internal;
5-
using Microsoft.EntityFrameworkCore.Metadata.Internal;
65

76
// ReSharper disable once CheckNamespace
87
namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal;
@@ -44,7 +43,7 @@ public ObjectArrayAccessExpression(
4443
break;
4544
case IComplexProperty complexProperty:
4645
targetType = complexProperty.ComplexType;
47-
propertyName = complexProperty.Name;
46+
propertyName = complexProperty.GetJsonPropertyName();
4847
break;
4948
default:
5049
throw new UnreachableException($"Unexpected structural property type: {structuralProperty.GetType().FullName}");

0 commit comments

Comments
 (0)