From 64b8b40cba8a13048bd4383acd69b4c91fc30aa1 Mon Sep 17 00:00:00 2001 From: Smit Patel Date: Tue, 31 Dec 2019 16:31:59 -0800 Subject: [PATCH] Metadata: Disallow backing field on indexer property - Don't discover backing field for indexer property by convention - Throw if trying to set a backing field for indexer property Resolves #19448 --- src/EFCore/Metadata/Internal/PropertyBase.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/EFCore/Metadata/Internal/PropertyBase.cs b/src/EFCore/Metadata/Internal/PropertyBase.cs index 4c96274ac7b..8e3577aeda8 100644 --- a/src/EFCore/Metadata/Internal/PropertyBase.cs +++ b/src/EFCore/Metadata/Internal/PropertyBase.cs @@ -148,6 +148,14 @@ public virtual void SetField([CanBeNull] FieldInfo fieldInfo, ConfigurationSourc return; } + if (PropertyInfo != null + && PropertyInfo.IsIndexerProperty() + && fieldInfo != null) + { + throw new InvalidOperationException( + CoreStrings.BackingFieldOnIndexer(fieldInfo.GetSimpleMemberName(), DeclaringType.DisplayName(), Name)); + } + if (fieldInfo != null) { IsCompatible(fieldInfo, ClrType, DeclaringType.ClrType, Name, shouldThrow: true);