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

[6.0.1] Avoid NRE in ForeignKeyAttributeConvention #26520

Merged
merged 1 commit into from
Nov 9, 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
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ var fkPropertiesOnDependentToPrincipal
}
}

return newRelationshipBuilder.HasForeignKey(fkPropertiesToSet, fromDataAnnotation: true);
return newRelationshipBuilder?.HasForeignKey(fkPropertiesToSet, fromDataAnnotation: true);
}

private static IConventionForeignKeyBuilder? SplitNavigationsToSeparateRelationships(
Expand Down
19 changes: 19 additions & 0 deletions test/EFCore.Specification.Tests/DataAnnotationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,25 @@ protected class Profile4
public virtual Login4 User { get; set; }
}

[ConditionalFact]
public virtual void Required_and_ForeignKey_to_ForeignKey_can_be_overridden()
{
var modelBuilder = CreateModelBuilder();

modelBuilder.Entity<Login3>()
.HasOne(p => p.Profile)
.WithOne(p => p.User)
.HasForeignKey<Login3>("ProfileId");

var model = Validate(modelBuilder);

var loginFk = GetProperty<Login3>(model, "ProfileId").GetContainingForeignKeys().Single();
Assert.True(loginFk.IsRequired); // This will be False after #15898 is fixed
Assert.True(loginFk.IsRequiredDependent);

Assert.False(GetProperty<Profile3>(model, nameof(Profile3.Profile3Id)).IsForeignKey());
}

[ConditionalFact]
public virtual void ForeignKey_to_nothing()
{
Expand Down