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

Don't try to discover FK properties when conflicting with 2 or more other FKs #29849

Merged
merged 1 commit into from
Dec 15, 2022
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 @@ -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