diff --git a/src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs b/src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs index 191c53ebe52..5828f930dc5 100644 --- a/src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs +++ b/src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs @@ -930,11 +930,11 @@ public void Attach(InternalEntityTypeBuilder entityTypeBuilder) } } - private List FindConflictingRelationships( + private IEnumerable FindConflictingRelationships( EntityType baseEntityType, ConfigurationSource configurationSource) { - var relationshipsToBeDetached = new List(); + var relationshipsToBeDetached = new HashSet(); foreach (var navigation in Metadata.GetDerivedNavigationsInclusive()) { if (!navigation.ForeignKey.GetConfigurationSource().Overrides( @@ -1739,16 +1739,16 @@ private InternalRelationshipBuilder Owns( var existingNavigation = Metadata .FindNavigationsInHierarchy(navigation.Name) .SingleOrDefault(n => n.GetTargetType().Name == targetEntityType.Name && n.GetTargetType().HasDefiningNavigation()); - + var builder = existingNavigation?.ForeignKey.Builder; - + if (builder != null) { builder = builder.RelatedEntityTypes(Metadata, existingNavigation.GetTargetType(), configurationSource); builder = builder?.IsRequired(true, configurationSource); builder = builder?.IsOwnership(true, configurationSource); builder = builder?.Navigations(inverse, navigation, configurationSource); - + return builder == null ? null : batch.Run(builder); } diff --git a/test/EFCore.Tests/ModelBuilding/InheritanceTestBase.cs b/test/EFCore.Tests/ModelBuilding/InheritanceTestBase.cs index 85217d8c061..22d8e1e6d04 100644 --- a/test/EFCore.Tests/ModelBuilding/InheritanceTestBase.cs +++ b/test/EFCore.Tests/ModelBuilding/InheritanceTestBase.cs @@ -8,6 +8,7 @@ using Microsoft.EntityFrameworkCore.TestUtilities; using Xunit; +// ReSharper disable InconsistentNaming namespace Microsoft.EntityFrameworkCore.ModelBuilding { public abstract partial class ModelBuilderTest @@ -68,6 +69,22 @@ public virtual void Base_types_are_mapped_correctly_if_discovered_last() Assert.Same(derived, moreDerived.BaseType); } + [Fact] + public virtual void Can_map_derived_self_ref_many_to_one() + { + var modelBuilder = CreateModelBuilder(); + + modelBuilder.Entity(); + modelBuilder.Entity(); + + modelBuilder.Validate(); + + var model = modelBuilder.Model; + Assert.Equal(0, model.FindEntityType(typeof(SelfRefManyToOneDerived)).GetDeclaredProperties().Count()); + Assert.NotNull(model.FindEntityType(typeof(SelfRefManyToOne)).FindNavigation(nameof(SelfRefManyToOne.SelfRef1))); + Assert.NotNull(model.FindEntityType(typeof(SelfRefManyToOne)).FindNavigation(nameof(SelfRefManyToOne.SelfRef2))); + } + [Fact] public virtual void Can_set_and_remove_base_type() { diff --git a/test/EFCore.Tests/ModelBuilding/TestModel.cs b/test/EFCore.Tests/ModelBuilding/TestModel.cs index 6160b33e666..fb7fd344e21 100644 --- a/test/EFCore.Tests/ModelBuilding/TestModel.cs +++ b/test/EFCore.Tests/ModelBuilding/TestModel.cs @@ -222,6 +222,18 @@ protected class SelfRef public int SelfRefId { get; set; } } + protected class SelfRefManyToOne + { + public int Id { get; set; } + public SelfRefManyToOne SelfRef1 { get; set; } + public ICollection SelfRef2 { get; set; } + public int SelfRefId { get; set; } + } + + protected class SelfRefManyToOneDerived : SelfRefManyToOne + { + } + protected class Book { public static readonly PropertyInfo BookDetailsNavigation = typeof(Book).GetProperty("Details");