Skip to content

Commit

Permalink
Don't try to discover FK properties when conflicting with 2 or more o…
Browse files Browse the repository at this point in the history
…ther FKs

Fixes #29826
  • Loading branch information
AndriySvyryd committed Dec 14, 2022
1 parent 4a2118f commit 806dce3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ private IConventionForeignKeyBuilder ProcessForeignKey(
: relationshipBuilder;
}

if (conflictingFKCount == 0)
if (conflictingFKCount >= 0)
{
return ((ForeignKey)foreignKey).Builder.ReuniquifyImplicitProperties(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ public void Does_not_match_if_a_foreign_key_on_the_best_candidate_property_alrea
dependentTypeBuilder.Property(DependentEntity.PrincipalEntityPeEKaYProperty, ConfigurationSource.Convention);
dependentTypeBuilder.Property(DependentEntity.PeEKaYProperty, ConfigurationSource.Convention);

dependentTypeBuilder.HasRelationship(PrincipalType, new[] { fkProperty }, ConfigurationSource.Convention);
var relationshipBuilder = dependentTypeBuilder.HasRelationship(PrincipalType, new[] { fkProperty }, ConfigurationSource.Convention);

var newRelationshipBuilder = dependentTypeBuilder.HasRelationship(
PrincipalType, "SomeNav", null, ConfigurationSource.Convention);
Expand All @@ -788,10 +788,6 @@ public void Does_not_match_if_a_foreign_key_on_the_best_candidate_property_alrea

newRelationshipBuilder = RunConvention(newRelationshipBuilder);

var relationshipBuilder = DependentType.GetForeignKeys()
.Single(foreignKey => foreignKey != newRelationshipBuilder.Metadata)
.Builder.HasForeignKey(new[] { fkProperty }, ConfigurationSource.Convention);

var fk = (IReadOnlyForeignKey)relationshipBuilder.Metadata;
Assert.Same(fkProperty, fk.Properties.Single());
Assert.False(fk.IsUnique);
Expand All @@ -808,7 +804,45 @@ public void Does_not_match_if_a_foreign_key_on_the_best_candidate_property_alrea
nameof(DependentEntity) + ".SomeNav",
nameof(PrincipalEntity),
"{'" + nameof(DependentEntity.SomeNavPeEKaY) + "'}"),
Assert.Throws<InvalidOperationException>(() => ValidateModel()).Message);
Assert.Throws<InvalidOperationException>(ValidateModel).Message);
}

[ConditionalFact]
public void Does_not_match_if_a_foreign_key_on_the_best_candidate_property_already_configured_explicitly()
{
var dependentTypeBuilder = DependentType.Builder;
var fkProperty = dependentTypeBuilder.Property(DependentEntity.SomeNavPeEKaYProperty, ConfigurationSource.Convention).Metadata;
dependentTypeBuilder.Property(DependentEntity.PrincipalEntityIDProperty, ConfigurationSource.Convention);
dependentTypeBuilder.Property(DependentEntity.PrincipalEntityPeEKaYProperty, ConfigurationSource.Convention);
dependentTypeBuilder.Property(DependentEntity.PeEKaYProperty, ConfigurationSource.Convention);

var derivedTypeBuilder = _model.Entity(typeof(DerivedPrincipalEntity), ConfigurationSource.Convention);
derivedTypeBuilder.HasBaseType(PrincipalType, ConfigurationSource.Convention);

var relationshipBuilder = dependentTypeBuilder
.HasRelationship(derivedTypeBuilder.Metadata, new[] { fkProperty }, ConfigurationSource.Explicit);
var compositeRelationshipBuilder = dependentTypeBuilder
.HasRelationship(PrincipalTypeWithCompositeKey, new[] { fkProperty }, ConfigurationSource.Explicit);

var newRelationshipBuilder = dependentTypeBuilder.HasRelationship(
PrincipalType, "SomeNav", null, ConfigurationSource.Convention);

Assert.Equal(
"SomeNav" + nameof(PrincipalEntity.PeeKay),
newRelationshipBuilder.Metadata.Properties.Single().Name);

newRelationshipBuilder = RunConvention(newRelationshipBuilder);

var fk = (IReadOnlyForeignKey)relationshipBuilder.Metadata;
Assert.Same(fkProperty, fk.Properties.Single());
Assert.False(fk.IsUnique);

var newFk = newRelationshipBuilder.Metadata;
Assert.Equal(3, DependentType.GetForeignKeys().Count());
Assert.Equal("SomeNav" + nameof(PrincipalEntity.PeeKay), newFk.Properties.Single().Name);
Assert.Null(newFk.GetPropertiesConfigurationSource());

ValidateModel();
}

[ConditionalFact]
Expand Down

0 comments on commit 806dce3

Please sign in to comment.