-
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.
Ensure that, by convention, dependent properties have the same elemen…
…t 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.
- Loading branch information
1 parent
e84e5ad
commit 57edad3
Showing
6 changed files
with
301 additions
and
10 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
58 changes: 58 additions & 0 deletions
58
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,58 @@ | ||
// 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 | ||
{ | ||
/// <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.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].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.