Skip to content

Commit

Permalink
Detect ambigous InversePropertyAttribute on collection navigations. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd authored Mar 30, 2024
1 parent 6df2608 commit b60fd47
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
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
{
}
}

0 comments on commit b60fd47

Please sign in to comment.