Skip to content

Commit

Permalink
Throw better exception for many-to-many with same navigation on both …
Browse files Browse the repository at this point in the history
…ends (#25990)

Co-authored-by: Andriy Svyryd <AndriySvyryd@users.noreply.github.com>
  • Loading branch information
ajcvickers and AndriySvyryd authored Sep 14, 2021
1 parent b47ddd1 commit d07f279
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/EFCore/Metadata/Builders/CollectionCollectionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.ComponentModel;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;
Expand Down Expand Up @@ -41,6 +42,12 @@ public CollectionCollectionBuilder(
Check.DebugAssert(((IConventionSkipNavigation)leftNavigation).IsInModel, "Not in model");
Check.DebugAssert(((IConventionSkipNavigation)rightNavigation).IsInModel, "Not in model");

if (leftNavigation == rightNavigation)
{
throw new InvalidOperationException(
CoreStrings.ManyToManyOneNav(leftEntityType.DisplayName(), leftNavigation.Name));
}

LeftEntityType = leftEntityType;
RightEntityType = rightEntityType;
LeftNavigation = leftNavigation;
Expand Down
8 changes: 8 additions & 0 deletions src/EFCore/Properties/CoreStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/EFCore/Properties/CoreStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,9 @@
<value>'{contextType}' generated value '{keyValue}' for the property '{3_entityType}.{2_property}'.</value>
<comment>Debug CoreEventId.ValueGenerated string object? string string</comment>
</data>
<data name="ManyToManyOneNav" xml:space="preserve">
<value>The navigation '{entityType}.{navigation}' cannot be used for both sides of a many-to-many relationship. Many-to-many relationships must use two distinct navigation properties.</value>
</data>
<data name="MissingBackingField" xml:space="preserve">
<value>The specified field '{field}' could not be found for property '{2_entityType}.{1_property}'.</value>
</data>
Expand Down
13 changes: 7 additions & 6 deletions test/EFCore.Tests/ModelBuilding/ManyToManyTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,13 @@ public virtual void Throws_for_self_ref_with_same_navigation()
{
var modelBuilder = CreateModelBuilder();

modelBuilder.Entity<SelfRefManyToOne>().Ignore(s => s.SelfRef1);
modelBuilder.Entity<SelfRefManyToOne>().HasMany(t => t.SelfRef2)
.WithMany(t => t.SelfRef2);

Assert.Equal(CoreStrings.EntityRequiresKey("SelfRefManyToOneSelfRefManyToOne (Dictionary<string, object>)"),
Assert.Throws<InvalidOperationException>(() => modelBuilder.FinalizeModel()).Message);
Assert.Equal(
CoreStrings.ManyToManyOneNav(nameof(SelfRefManyToOne), nameof(SelfRefManyToOne.SelfRef2)),
Assert.Throws<InvalidOperationException>(
() => modelBuilder
.Entity<SelfRefManyToOne>()
.HasMany(e => e.SelfRef2)
.WithMany(e => e.SelfRef2)).Message);
}

[ConditionalFact]
Expand Down

0 comments on commit d07f279

Please sign in to comment.