From 3a1fd2df4572cecc2d130dbbdf61d93f87304fcd Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Tue, 22 Sep 2020 11:14:47 -0700 Subject: [PATCH] Don't remove skip foreign key when join entity type is configured explicitly. (#22652) Fixes #22521 --- src/EFCore/Metadata/Conventions/ModelCleanupConvention.cs | 3 ++- test/EFCore.Tests/ModelBuilding/ManyToManyTestBase.cs | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/EFCore/Metadata/Conventions/ModelCleanupConvention.cs b/src/EFCore/Metadata/Conventions/ModelCleanupConvention.cs index 33ffd9e2059..20320625645 100644 --- a/src/EFCore/Metadata/Conventions/ModelCleanupConvention.cs +++ b/src/EFCore/Metadata/Conventions/ModelCleanupConvention.cs @@ -79,7 +79,8 @@ private void RemoveNavigationlessForeignKeys(IConventionModelBuilder modelBuilde foreach (var foreignKey in entityType.GetDeclaredForeignKeys().ToList()) { if (foreignKey.PrincipalToDependent == null - && foreignKey.DependentToPrincipal == null) + && foreignKey.DependentToPrincipal == null + && !foreignKey.GetReferencingSkipNavigations().Any()) { entityType.Builder.HasNoRelationship(foreignKey, fromDataAnnotation: true); } diff --git a/test/EFCore.Tests/ModelBuilding/ManyToManyTestBase.cs b/test/EFCore.Tests/ModelBuilding/ManyToManyTestBase.cs index 6e2645fcf43..edf69db3f69 100644 --- a/test/EFCore.Tests/ModelBuilding/ManyToManyTestBase.cs +++ b/test/EFCore.Tests/ModelBuilding/ManyToManyTestBase.cs @@ -26,6 +26,10 @@ public virtual void Discovers_navigations() modelBuilder.Entity(); modelBuilder.Entity(); + var sharedTypeName = nameof(Category) + nameof(Product); + + modelBuilder.SharedTypeEntity>(sharedTypeName); + var model = modelBuilder.FinalizeModel(); var productType = model.FindEntityType(typeof(Product)); @@ -39,7 +43,7 @@ public virtual void Discovers_navigations() var productCategoryType = categoriesFk.DeclaringEntityType; Assert.Equal(typeof(Dictionary), productCategoryType.ClrType); - Assert.Equal(nameof(Category) + nameof(Product), productCategoryType.Name); + Assert.Equal(sharedTypeName, productCategoryType.Name); Assert.Same(categoriesFk, productCategoryType.GetForeignKeys().Last()); Assert.Same(productsFk, productCategoryType.GetForeignKeys().First()); Assert.Equal(2, productCategoryType.GetForeignKeys().Count());