Skip to content

Commit

Permalink
Configure requiredness based on NRT even if principal end might be am…
Browse files Browse the repository at this point in the history
…biguous.

Fixes #23588
  • Loading branch information
AndriySvyryd committed Sep 1, 2021
1 parent b820df4 commit ca3c45e
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,13 @@ private void ProcessNavigation(IConventionNavigationBuilder navigationBuilder)
return;
}

if (foreignKey.GetPrincipalEndConfigurationSource() != null)
if (navigation.IsOnDependent)
{
if (navigation.IsOnDependent)
{
foreignKey.Builder.IsRequired(true);
}
else
{
foreignKey.Builder.IsRequiredDependent(true);
}
foreignKey.Builder.IsRequired(true);
}
else
{
foreignKey.Builder.IsRequiredDependent(true);
}
}

Expand Down
8 changes: 5 additions & 3 deletions test/EFCore.Tests/ModelBuilding/InheritanceTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,17 @@ public virtual void Can_map_derived_self_ref_many_to_one()
var modelBuilder = CreateModelBuilder();

modelBuilder.Entity<SelfRefManyToOneDerived>().HasData(
new SelfRefManyToOneDerived { Id = 1, SelfRefId = 1 });
new SelfRefManyToOneDerived { Id = 1, SelfRef1Id = 1 });
modelBuilder.Entity<SelfRefManyToOne>();

modelBuilder.FinalizeModel();

var model = modelBuilder.Model;
Assert.Empty(model.FindEntityType(typeof(SelfRefManyToOneDerived)).GetDeclaredProperties());
Assert.NotNull(model.FindEntityType(typeof(SelfRefManyToOne)).FindNavigation(nameof(SelfRefManyToOne.SelfRef1)));
Assert.NotNull(model.FindEntityType(typeof(SelfRefManyToOne)).FindNavigation(nameof(SelfRefManyToOne.SelfRef2)));
var fk = model.FindEntityType(typeof(SelfRefManyToOne)).FindNavigation(nameof(SelfRefManyToOne.SelfRef1)).ForeignKey;
Assert.Equal(nameof(SelfRefManyToOne.SelfRef2), fk.PrincipalToDependent.Name);
Assert.Equal(nameof(SelfRefManyToOne.SelfRef1Id), fk.Properties.Single().Name);
Assert.True(fk.IsRequired);
}

[ConditionalFact]
Expand Down
3 changes: 3 additions & 0 deletions test/EFCore.Tests/ModelBuilding/OneToOneTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2572,6 +2572,7 @@ public virtual void Principal_and_dependent_can_be_flipped_when_self_referencing
Assert.Equal(navigationToPrincipal.Name, fk.PrincipalToDependent.Name);
Assert.Equal(navigationToDependent?.Name, fk.DependentToPrincipal?.Name);
Assert.True(fk.IsRequired);
Assert.False(fk.IsRequiredDependent);

Assert.Equal(fk.DeclaringEntityType.GetForeignKeys().Count(), fk.DeclaringEntityType.GetIndexes().Count());
Assert.True(fk.DeclaringEntityType.FindIndex(fk.Properties).IsUnique);
Expand Down Expand Up @@ -2634,6 +2635,8 @@ public virtual void Creates_self_referencing_FK_by_convention()
var entityType = modelBuilder.Model.FindEntityType(typeof(SelfRef));
var fk = entityType.GetForeignKeys().Single();
Assert.Equal("SelfRef1Id", fk.Properties.Single().Name);
Assert.True(fk.IsRequired);
Assert.False(fk.IsRequiredDependent);
Assert.Equal(fk.PrincipalKey, entityType.FindPrimaryKey());
Assert.Equal(nameof(SelfRef.SelfRef2), fk.PrincipalToDependent?.Name);
Assert.Equal(nameof(SelfRef.SelfRef1), fk.DependentToPrincipal?.Name);
Expand Down
4 changes: 4 additions & 0 deletions test/EFCore.Tests/ModelBuilding/OwnedTypesTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,10 +534,12 @@ public virtual void Can_call_Owner_fluent_api_after_calling_Entity()
[Flags]
public enum HasDataOverload
{
#pragma warning disable SA1602 // Enumeration items should be documented
Array = 0,
Enumerable = 1,
Generic = 2,
Params = 4
#pragma warning restore SA1602 // Enumeration items should be documented
}

[ConditionalTheory]
Expand Down Expand Up @@ -875,6 +877,8 @@ public virtual void Can_configure_single_owned_type_using_attribute()
var owner = model.FindEntityType(typeof(SpecialOrder));
var ownership = owner.FindNavigation(nameof(SpecialOrder.ShippingAddress)).ForeignKey;
Assert.True(ownership.IsOwnership);
Assert.True(ownership.IsRequired);
Assert.True(ownership.IsRequiredDependent);
Assert.NotNull(ownership.DeclaringEntityType.FindProperty(nameof(StreetAddress.Street)));
}

Expand Down
Loading

0 comments on commit ca3c45e

Please sign in to comment.