Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw better exception for many-to-many with same navigation on both ends #25990

Merged
merged 2 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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