Skip to content

Commit

Permalink
Don't try to detach the same relationship twice.
Browse files Browse the repository at this point in the history
Fix #9704
  • Loading branch information
AndriySvyryd committed Sep 8, 2017
1 parent 99a915e commit 0eee7bf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/EFCore/Metadata/Internal/InternalEntityTypeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -930,11 +930,11 @@ public void Attach(InternalEntityTypeBuilder entityTypeBuilder)
}
}

private List<ForeignKey> FindConflictingRelationships(
private IEnumerable<ForeignKey> FindConflictingRelationships(
EntityType baseEntityType,
ConfigurationSource configurationSource)
{
var relationshipsToBeDetached = new List<ForeignKey>();
var relationshipsToBeDetached = new HashSet<ForeignKey>();
foreach (var navigation in Metadata.GetDerivedNavigationsInclusive())
{
if (!navigation.ForeignKey.GetConfigurationSource().Overrides(
Expand Down Expand Up @@ -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);
}

Expand Down
17 changes: 17 additions & 0 deletions test/EFCore.Tests/ModelBuilding/InheritanceTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.EntityFrameworkCore.TestUtilities;
using Xunit;

// ReSharper disable InconsistentNaming
namespace Microsoft.EntityFrameworkCore.ModelBuilding
{
public abstract partial class ModelBuilderTest
Expand Down Expand Up @@ -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<SelfRefManyToOneDerived>();
modelBuilder.Entity<SelfRefManyToOne>();

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()
{
Expand Down
12 changes: 12 additions & 0 deletions test/EFCore.Tests/ModelBuilding/TestModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SelfRefManyToOne> SelfRef2 { get; set; }
public int SelfRefId { get; set; }
}

protected class SelfRefManyToOneDerived : SelfRefManyToOne
{
}

protected class Book
{
public static readonly PropertyInfo BookDetailsNavigation = typeof(Book).GetProperty("Details");
Expand Down

0 comments on commit 0eee7bf

Please sign in to comment.