Skip to content

Commit

Permalink
[release/8.0] Update FK ElementType when the FK properties change. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd committed Sep 3, 2024
1 parent e49bbfb commit b2e366b
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 56 deletions.
24 changes: 24 additions & 0 deletions src/EFCore.Cosmos/Storage/Internal/CosmosTypeMappingSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.ObjectModel;
using Microsoft.EntityFrameworkCore.Cosmos.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Storage.Json;
using Newtonsoft.Json.Linq;

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

internal static readonly bool UseOldBehavior33704 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue33704", out var enabled33704) && enabled33704;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down Expand Up @@ -69,6 +73,26 @@ public CosmosTypeMappingSource(TypeMappingSourceDependencies dependencies)
return null;
}

/// <inheritdoc/>
protected override bool TryFindJsonCollectionMapping(
TypeMappingInfo mappingInfo,
Type modelClrType,
Type? providerClrType,
ref CoreTypeMapping? elementMapping,
out ValueComparer? elementComparer,
out JsonValueReaderWriter? collectionReaderWriter)
{
if (UseOldBehavior33704)
{
return base.TryFindJsonCollectionMapping(
mappingInfo, modelClrType, providerClrType, ref elementMapping, out elementComparer, out collectionReaderWriter);
}

elementComparer = null;
collectionReaderWriter = null;
return false;
}

private CoreTypeMapping? FindCollectionMapping(in TypeMappingInfo mappingInfo)
{
var clrType = mappingInfo.ClrType!;
Expand Down
23 changes: 22 additions & 1 deletion src/EFCore/Metadata/Conventions/ElementTypeChangedConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Conventions;
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-conventions">Model building conventions</see> for more information and examples.
/// </remarks>
public class ElementTypeChangedConvention : IPropertyElementTypeChangedConvention, IForeignKeyAddedConvention
public class ElementTypeChangedConvention :
IPropertyElementTypeChangedConvention, IForeignKeyAddedConvention, IForeignKeyPropertiesChangedConvention
{
internal static readonly bool UseOldBehavior32411 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue32411", out var enabled32411) && enabled32411;

internal static readonly bool UseOldBehavior33704 =
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue33704", out var enabled33704) && enabled33704;

/// <summary>
/// Creates a new instance of <see cref="ElementTypeChangedConvention" />.
/// </summary>
Expand Down Expand Up @@ -50,6 +54,23 @@ public void ProcessPropertyElementTypeChanged(
/// <inheritdoc />
public void ProcessForeignKeyAdded(
IConventionForeignKeyBuilder foreignKeyBuilder, IConventionContext<IConventionForeignKeyBuilder> context)
=> ProcessForeignKey(foreignKeyBuilder);

/// <inheritdoc />
public void ProcessForeignKeyPropertiesChanged(
IConventionForeignKeyBuilder relationshipBuilder,
IReadOnlyList<IConventionProperty> oldDependentProperties,
IConventionKey oldPrincipalKey,
IConventionContext<IReadOnlyList<IConventionProperty>> context)
{
if (relationshipBuilder.Metadata.IsInModel
&& !UseOldBehavior33704)
{
ProcessForeignKey(relationshipBuilder);
}
}

private static void ProcessForeignKey(IConventionForeignKeyBuilder foreignKeyBuilder)
{
var foreignKeyProperties = foreignKeyBuilder.Metadata.Properties;
var principalKeyProperties = foreignKeyBuilder.Metadata.PrincipalKey.Properties;
Expand Down
8 changes: 8 additions & 0 deletions src/EFCore/Metadata/Internal/InternalPropertyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,14 @@ public virtual bool CanSetProviderValueComparer(
{
Metadata.SetValueConverter((Type?)null, configurationSource);
}

if (elementType == null
&& CanSetConversion((Type?)null, configurationSource)
&& !ElementTypeChangedConvention.UseOldBehavior33704)
{
Metadata.RemoveAnnotation(CoreAnnotationNames.ValueConverter);
}

return new InternalElementTypeBuilder(Metadata.GetElementType()!, ModelBuilder);
}

Expand Down
Loading

0 comments on commit b2e366b

Please sign in to comment.