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] Update the foreign key builder after IsRequired() #26719

Merged
merged 2 commits into from
Nov 17, 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
14 changes: 13 additions & 1 deletion src/EFCore/Metadata/Internal/InternalForeignKeyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,19 @@ private static bool IsCompatible(
Metadata.DeclaringEntityType.Model.ScopedModelDependencies?.Logger.AmbiguousEndRequiredWarning(Metadata);
}

Metadata.SetIsRequired(required, configurationSource);
if (AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue26611", out var enabled) && enabled)
{
Metadata.SetIsRequired(required, configurationSource);
return this;
}

IConventionForeignKey? foreignKey = Metadata;
var result = Metadata.DeclaringEntityType.Model.ConventionDispatcher.Track(
() => Metadata.SetIsRequired(required, configurationSource), ref foreignKey);
if (foreignKey != null)
{
return ((ForeignKey)foreignKey).Builder;
}

return this;
}
Expand Down
13 changes: 8 additions & 5 deletions test/EFCore.Tests/ModelBuilding/ManyToOneTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1942,14 +1942,17 @@ public virtual void Creates_shadow_FK_property_with_non_shadow_PK()
{
b.HasMany<Beta>()
.WithOne(e => e.FirstNav)
.HasForeignKey("ShadowId");
.HasForeignKey("ShadowId")
.IsRequired()
.HasAnnotation("Test", "foo");
});

modelBuilder.FinalizeModel();
var model = modelBuilder.FinalizeModel();

Assert.Equal(
"ShadowId",
modelBuilder.Model.FindEntityType(typeof(Beta)).FindNavigation("FirstNav").ForeignKey.Properties.Single().Name);
var fk = model.FindEntityType(typeof(Beta)).FindNavigation("FirstNav").ForeignKey;
Assert.Equal("ShadowId", fk.Properties.Single().Name);
Assert.True(fk.IsRequired);
Assert.Equal("foo", fk["Test"]);
}

[ConditionalFact]
Expand Down