From 8b61f64df1b30375d00634d481c4f9ce5c21b665 Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Thu, 11 Nov 2021 21:44:05 -0800 Subject: [PATCH 1/3] Match indexer properties correctly Fixes #26590 --- src/EFCore/Metadata/Internal/Property.cs | 10 +++-- .../DataAnnotationTestBase.cs | 45 +++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/EFCore/Metadata/Internal/Property.cs b/src/EFCore/Metadata/Internal/Property.cs index 2a398e1dff8..e73bcc78480 100644 --- a/src/EFCore/Metadata/Internal/Property.cs +++ b/src/EFCore/Metadata/Internal/Property.cs @@ -999,10 +999,12 @@ public static bool AreCompatible(IReadOnlyList properties, EntityType => properties.All( property => property.IsShadowProperty() - || ((property.PropertyInfo != null - && entityType.GetRuntimeProperties().ContainsKey(property.Name)) - || (property.FieldInfo != null - && entityType.GetRuntimeFields().ContainsKey(property.Name)))); + || (property.IsIndexerProperty() + ? property.PropertyInfo == entityType.FindIndexerPropertyInfo() + : ((property.PropertyInfo != null + && entityType.GetRuntimeProperties().ContainsKey(property.Name)) + || (property.FieldInfo != null + && entityType.GetRuntimeFields().ContainsKey(property.Name))))); /// /// This is an internal API that supports the Entity Framework Core infrastructure and not subject to diff --git a/test/EFCore.Specification.Tests/DataAnnotationTestBase.cs b/test/EFCore.Specification.Tests/DataAnnotationTestBase.cs index 4bb01051d1f..8093d2131d0 100644 --- a/test/EFCore.Specification.Tests/DataAnnotationTestBase.cs +++ b/test/EFCore.Specification.Tests/DataAnnotationTestBase.cs @@ -1579,6 +1579,51 @@ protected class Profile15 public virtual Profile15 Profile4 { get; set; } } + [ConditionalFact] + public virtual void ForeignKey_to_ForeignKey_on_many_to_many() + { + var modelBuilder = CreateModelBuilder(); + + modelBuilder.Entity(entity => + { + entity.HasMany(d => d.Profile16s) + .WithMany(p => p.Login16s) + .UsingEntity>( + "Login16Profile16", + l => l.HasOne().WithMany().HasForeignKey("Profile16Id"), + r => r.HasOne().WithMany().HasForeignKey("Login16Id"), + j => + { + j.HasKey("Login16Id", "Profile16Id"); + + j.ToTable("Login16Profile16"); + }); + }); + + var model = Validate(modelBuilder); + + var login = modelBuilder.Model.FindEntityType(typeof(Login16)); + var logins = login.FindSkipNavigation(nameof(Login16.Profile16s)); + var join = logins.JoinEntityType; + Assert.Equal(2, join.GetProperties().Count()); + Assert.False(GetProperty(model, "Login16Id").IsForeignKey()); + Assert.False(GetProperty(model, "Profile16Id").IsForeignKey()); + } + + public partial class Login16 + { + public int Login16Id { get; set; } + [ForeignKey("Login16Id")] + public virtual ICollection Profile16s { get; set; } + } + + public partial class Profile16 + { + public int Profile16Id { get; set; } + [ForeignKey("Profile16Id")] + public virtual ICollection Login16s { get; set; } + } + [ConditionalFact] public virtual void ForeignKeyAttribute_configures_relationships_when_inverse_on_derived() { From 375a527dcc51dc96fd3b13f9fe18080a195dac24 Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Thu, 11 Nov 2021 21:51:04 -0800 Subject: [PATCH 2/3] Add quirk mode for #26590 --- src/EFCore/Metadata/Internal/Property.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/EFCore/Metadata/Internal/Property.cs b/src/EFCore/Metadata/Internal/Property.cs index e73bcc78480..a30a1545c20 100644 --- a/src/EFCore/Metadata/Internal/Property.cs +++ b/src/EFCore/Metadata/Internal/Property.cs @@ -1000,6 +1000,7 @@ public static bool AreCompatible(IReadOnlyList properties, EntityType property => property.IsShadowProperty() || (property.IsIndexerProperty() + && (!AppContext.TryGetSwitch("Microsoft.Data.Sqlite.Issue26590", out var enabled) || !enabled) ? property.PropertyInfo == entityType.FindIndexerPropertyInfo() : ((property.PropertyInfo != null && entityType.GetRuntimeProperties().ContainsKey(property.Name)) From 0ee7e7cba940eaca0a8bca143b8d88c2d5c8840f Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Fri, 12 Nov 2021 10:13:46 -0800 Subject: [PATCH 3/3] Update src/EFCore/Metadata/Internal/Property.cs Co-authored-by: Shay Rojansky --- src/EFCore/Metadata/Internal/Property.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EFCore/Metadata/Internal/Property.cs b/src/EFCore/Metadata/Internal/Property.cs index a30a1545c20..6976be4429b 100644 --- a/src/EFCore/Metadata/Internal/Property.cs +++ b/src/EFCore/Metadata/Internal/Property.cs @@ -1000,7 +1000,7 @@ public static bool AreCompatible(IReadOnlyList properties, EntityType property => property.IsShadowProperty() || (property.IsIndexerProperty() - && (!AppContext.TryGetSwitch("Microsoft.Data.Sqlite.Issue26590", out var enabled) || !enabled) + && (!AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue26590", out var enabled) || !enabled) ? property.PropertyInfo == entityType.FindIndexerPropertyInfo() : ((property.PropertyInfo != null && entityType.GetRuntimeProperties().ContainsKey(property.Name))