-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release/8.0] Ensure that, by convention, dependent properties have t…
…he same element type as principal properties (#32582) * Ensure that, by convention, dependent properties have the same element type as principal properties (#32560) * Ensure that, by convention, dependent properties have the same element type as principal properties Fixes #32411 The issue here is that when a value converter is applied to a principal property, then that converter is used by the dependent properties unless something else is explicitly configured. However, this meant that when the PK has a converter but the FK does not, then the FK can get configured as a primitive collection, since it doesn't have a converter preventing this. The fix is to add a convention that sets the element type for dependent properties to match that for principal properties. * Update based on review * Qwerk
- Loading branch information
1 parent
7ee1aa2
commit 5b1c02e
Showing
8 changed files
with
323 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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 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
61 changes: 61 additions & 0 deletions
61
src/EFCore/Metadata/Conventions/ElementTypeChangedConvention.cs
This file contains 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,61 @@ | ||
// 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.Metadata.Conventions; | ||
|
||
/// <summary> | ||
/// A convention that reacts to changes made to element types of primitive collections. | ||
/// </summary> | ||
/// <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 | ||
{ | ||
internal static readonly bool UseOldBehavior32411 = | ||
AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue32411", out var enabled32411) && enabled32411; | ||
|
||
/// <summary> | ||
/// Creates a new instance of <see cref="ElementTypeChangedConvention" />. | ||
/// </summary> | ||
/// <param name="dependencies">Parameter object containing dependencies for this convention.</param> | ||
public ElementTypeChangedConvention(ProviderConventionSetBuilderDependencies dependencies) | ||
{ | ||
Dependencies = dependencies; | ||
} | ||
|
||
/// <summary> | ||
/// Dependencies for this service. | ||
/// </summary> | ||
protected virtual ProviderConventionSetBuilderDependencies Dependencies { get; } | ||
|
||
/// <inheritdoc /> | ||
public void ProcessPropertyElementTypeChanged( | ||
IConventionPropertyBuilder propertyBuilder, | ||
IElementType? newElementType, | ||
IElementType? oldElementType, | ||
IConventionContext<IElementType> context) | ||
{ | ||
var keyProperty = propertyBuilder.Metadata; | ||
foreach (var key in keyProperty.GetContainingKeys()) | ||
{ | ||
var index = key.Properties.IndexOf(keyProperty); | ||
foreach (var foreignKey in key.GetReferencingForeignKeys()) | ||
{ | ||
var foreignKeyProperty = foreignKey.Properties[index]; | ||
foreignKeyProperty.Builder.SetElementType(newElementType?.ClrType); | ||
} | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void ProcessForeignKeyAdded( | ||
IConventionForeignKeyBuilder foreignKeyBuilder, IConventionContext<IConventionForeignKeyBuilder> context) | ||
{ | ||
var foreignKeyProperties = foreignKeyBuilder.Metadata.Properties; | ||
var principalKeyProperties = foreignKeyBuilder.Metadata.PrincipalKey.Properties; | ||
for (var i = 0; i < foreignKeyProperties.Count; i++) | ||
{ | ||
foreignKeyProperties[i].Builder.SetElementType(principalKeyProperties[i].GetElementType()?.ClrType); | ||
} | ||
} | ||
} |
This file contains 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 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 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.