Skip to content
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

[release/8.0] Update FK ElementType when the FK properties change. #34561

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading