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

Detect ambiguous InversePropertyAttribute on collection navigations. #33434

Merged
merged 1 commit into from
Mar 30, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,8 @@ public static bool IsAmbiguous(
{
foreach (var (memberInfo, references) in navigationMap.Values)
{
if (memberInfo.GetMemberType().IsAssignableFrom(entityType.ClrType)
var memberInfoType = memberInfo.GetMemberType();
if ((memberInfoType.TryGetSequenceType() ?? memberInfoType).IsAssignableFrom(entityType.ClrType)
&& IsAmbiguousInverse(navigation, entityType, references))
{
return true;
Expand Down
26 changes: 24 additions & 2 deletions test/EFCore.Specification.Tests/DataAnnotationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2227,7 +2227,7 @@ public virtual void InversePropertyAttribute_pointing_to_same_skip_nav_on_base_c
{
var modelBuilder = CreateModelBuilder();
modelBuilder.Entity<AmbiguousInversePropertyLeft>();
modelBuilder.Entity<AmbiguousInversePropertyLeftDerived>();
modelBuilder.Entity<AmbiguousInversePropertyRightDerived>();

Assert.Equal(
CoreStrings.WarningAsErrorTemplate(
Expand All @@ -2249,7 +2249,6 @@ protected class AmbiguousInversePropertyLeft

protected class AmbiguousInversePropertyLeftDerived : AmbiguousInversePropertyLeft
{
public List<AmbiguousInversePropertyRightDerived> DerivedRights { get; set; }
}

protected class AmbiguousInversePropertyRight
Expand Down Expand Up @@ -2771,6 +2770,21 @@ public virtual void InverseProperty_with_case_sensitive_clr_property()
Validate(modelBuilder);
}

[ConditionalFact]
public virtual void InverseProperty_with_potentially_ambigous_derived_types()
{
var modelBuilder = CreateModelBuilder();
var model = modelBuilder.Model;

modelBuilder.Ignore<CPSorder>();
modelBuilder.Entity<SpecialOrder>();
modelBuilder.Entity<CPSpecialOrder>();

modelBuilder.Entity<CPSorder>().HasKey(e => e.Id);

Validate(modelBuilder);
}

public abstract class DataAnnotationFixtureBase : SharedStoreFixtureBase<PoolableDbContext>
{
protected override string StoreName
Expand Down Expand Up @@ -2929,4 +2943,12 @@ protected class Partner
[InverseProperty(nameof(CPSorder.CPSchargePartner))]
public virtual ICollection<CPSorder> CPSorders { get; set; }
}

protected class SpecialOrder : CPSorder
{
}

protected class CPSpecialOrder : CPSorder
{
}
}
Loading