Skip to content

Commit 0ec1568

Browse files
author
Mirroring
committed
Merge commit 'b2e366bf1d1955903c75c06288bad5bc5b8deda0'
2 parents 7edba26 + b2e366b commit 0ec1568

File tree

6 files changed

+144
-56
lines changed

6 files changed

+144
-56
lines changed

src/EFCore.Cosmos/Storage/Internal/CosmosTypeMappingSource.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.ObjectModel;
55
using Microsoft.EntityFrameworkCore.Cosmos.ChangeTracking.Internal;
6+
using Microsoft.EntityFrameworkCore.Storage.Json;
67
using Newtonsoft.Json.Linq;
78

89
namespace Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal;
@@ -17,6 +18,9 @@ public class CosmosTypeMappingSource : TypeMappingSource
1718
{
1819
private readonly Dictionary<Type, CosmosTypeMapping> _clrTypeMappings;
1920

21+
internal static readonly bool UseOldBehavior33704 =
22+
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue33704", out var enabled33704) && enabled33704;
23+
2024
/// <summary>
2125
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
2226
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
@@ -69,6 +73,26 @@ public CosmosTypeMappingSource(TypeMappingSourceDependencies dependencies)
6973
return null;
7074
}
7175

76+
/// <inheritdoc/>
77+
protected override bool TryFindJsonCollectionMapping(
78+
TypeMappingInfo mappingInfo,
79+
Type modelClrType,
80+
Type? providerClrType,
81+
ref CoreTypeMapping? elementMapping,
82+
out ValueComparer? elementComparer,
83+
out JsonValueReaderWriter? collectionReaderWriter)
84+
{
85+
if (UseOldBehavior33704)
86+
{
87+
return base.TryFindJsonCollectionMapping(
88+
mappingInfo, modelClrType, providerClrType, ref elementMapping, out elementComparer, out collectionReaderWriter);
89+
}
90+
91+
elementComparer = null;
92+
collectionReaderWriter = null;
93+
return false;
94+
}
95+
7296
private CoreTypeMapping? FindCollectionMapping(in TypeMappingInfo mappingInfo)
7397
{
7498
var clrType = mappingInfo.ClrType!;

src/EFCore/Metadata/Conventions/ElementTypeChangedConvention.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Conventions;
99
/// <remarks>
1010
/// See <see href="https://aka.ms/efcore-docs-conventions">Model building conventions</see> for more information and examples.
1111
/// </remarks>
12-
public class ElementTypeChangedConvention : IPropertyElementTypeChangedConvention, IForeignKeyAddedConvention
12+
public class ElementTypeChangedConvention :
13+
IPropertyElementTypeChangedConvention, IForeignKeyAddedConvention, IForeignKeyPropertiesChangedConvention
1314
{
1415
internal static readonly bool UseOldBehavior32411 =
1516
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue32411", out var enabled32411) && enabled32411;
1617

18+
internal static readonly bool UseOldBehavior33704 =
19+
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue33704", out var enabled33704) && enabled33704;
20+
1721
/// <summary>
1822
/// Creates a new instance of <see cref="ElementTypeChangedConvention" />.
1923
/// </summary>
@@ -50,6 +54,23 @@ public void ProcessPropertyElementTypeChanged(
5054
/// <inheritdoc />
5155
public void ProcessForeignKeyAdded(
5256
IConventionForeignKeyBuilder foreignKeyBuilder, IConventionContext<IConventionForeignKeyBuilder> context)
57+
=> ProcessForeignKey(foreignKeyBuilder);
58+
59+
/// <inheritdoc />
60+
public void ProcessForeignKeyPropertiesChanged(
61+
IConventionForeignKeyBuilder relationshipBuilder,
62+
IReadOnlyList<IConventionProperty> oldDependentProperties,
63+
IConventionKey oldPrincipalKey,
64+
IConventionContext<IReadOnlyList<IConventionProperty>> context)
65+
{
66+
if (relationshipBuilder.Metadata.IsInModel
67+
&& !UseOldBehavior33704)
68+
{
69+
ProcessForeignKey(relationshipBuilder);
70+
}
71+
}
72+
73+
private static void ProcessForeignKey(IConventionForeignKeyBuilder foreignKeyBuilder)
5374
{
5475
var foreignKeyProperties = foreignKeyBuilder.Metadata.Properties;
5576
var principalKeyProperties = foreignKeyBuilder.Metadata.PrincipalKey.Properties;

src/EFCore/Metadata/Internal/InternalPropertyBuilder.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,14 @@ public virtual bool CanSetProviderValueComparer(
802802
{
803803
Metadata.SetValueConverter((Type?)null, configurationSource);
804804
}
805+
806+
if (elementType == null
807+
&& CanSetConversion((Type?)null, configurationSource)
808+
&& !ElementTypeChangedConvention.UseOldBehavior33704)
809+
{
810+
Metadata.RemoveAnnotation(CoreAnnotationNames.ValueConverter);
811+
}
812+
805813
return new InternalElementTypeBuilder(Metadata.GetElementType()!, ModelBuilder);
806814
}
807815

0 commit comments

Comments
 (0)