Skip to content

Commit

Permalink
Fix scaffolding of reference navigations with NRT (#27616)
Browse files Browse the repository at this point in the history
Fixes #27496
  • Loading branch information
roji authored Mar 11, 2022
1 parent 96f7bb8 commit ec9c6c2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ protected virtual void GenerateNavigationProperties(IEntityType entityType)
_sb.AppendLine(
!_useNullableReferenceTypes || navigation.IsCollection
? $"public virtual {navigationType} {navigation.Name} {{ get; set; }}"
: navigation.ForeignKey.IsRequired
: navigation.ForeignKey.IsRequired && navigation.IsOnDependent
? $"public virtual {navigationType} {navigation.Name} {{ get; set; }} = null!;"
: $"public virtual {navigationType}? {navigation.Name} {{ get; set; }}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,10 +752,27 @@ public void Required_and_not_required_reference_navigations_with_nrt()
{
x.Property<int>("Id");
x.HasOne("Dependent1", "RequiredNavigationWithReferenceForeignKey").WithOne("Entity").IsRequired();
x.HasOne("Dependent2", "OptionalNavigationWithReferenceForeignKey").WithOne("Entity");
x.HasOne("Dependent3", "RequiredNavigationWithValueForeignKey").WithOne("Entity").IsRequired();
x.HasOne("Dependent4", "OptionalNavigationWithValueForeignKey").WithOne("Entity");
x
.HasOne("Dependent1", "RequiredNavigationWithReferenceForeignKey")
.WithOne("Entity")
.HasForeignKey("Dependent1", "RequiredNavigationWithReferenceForeignKey")
.IsRequired();
x
.HasOne("Dependent2", "OptionalNavigationWithReferenceForeignKey")
.WithOne("Entity")
.HasForeignKey("Dependent2", "OptionalNavigationWithReferenceForeignKey");
x
.HasOne("Dependent3", "RequiredNavigationWithValueForeignKey")
.WithOne("Entity")
.HasForeignKey("Dependent3", "RequiredNavigationWithValueForeignKey")
.IsRequired();
x
.HasOne("Dependent4", "OptionalNavigationWithValueForeignKey")
.WithOne("Entity")
.HasForeignKey("Dependent4", "OptionalNavigationWithValueForeignKey");
})
.Entity("Dependent1", x => x.Property<string>("Id"))
.Entity("Dependent2", x => x.Property<string>("Id"))
Expand All @@ -777,23 +794,15 @@ public partial class Entity
{
[Key]
public int Id { get; set; }
public string? OptionalNavigationWithReferenceForeignKeyId { get; set; }
public int? OptionalNavigationWithValueForeignKeyId { get; set; }
public string RequiredNavigationWithReferenceForeignKeyId { get; set; } = null!;
public int RequiredNavigationWithValueForeignKeyId { get; set; }
[ForeignKey(""OptionalNavigationWithReferenceForeignKeyId"")]
[InverseProperty(""Entity"")]
public virtual Dependent2? OptionalNavigationWithReferenceForeignKey { get; set; }
[ForeignKey(""OptionalNavigationWithValueForeignKeyId"")]
[InverseProperty(""Entity"")]
public virtual Dependent4? OptionalNavigationWithValueForeignKey { get; set; }
[ForeignKey(""RequiredNavigationWithReferenceForeignKeyId"")]
[InverseProperty(""Entity"")]
public virtual Dependent1 RequiredNavigationWithReferenceForeignKey { get; set; } = null!;
[ForeignKey(""RequiredNavigationWithValueForeignKeyId"")]
public virtual Dependent1? RequiredNavigationWithReferenceForeignKey { get; set; }
[InverseProperty(""Entity"")]
public virtual Dependent3 RequiredNavigationWithValueForeignKey { get; set; } = null!;
public virtual Dependent3? RequiredNavigationWithValueForeignKey { get; set; }
}
}
",
Expand All @@ -803,11 +812,6 @@ public partial class Entity
{
var entityType = model.FindEntityType("TestNamespace.Entity");
Assert.False(entityType.GetProperty("RequiredNavigationWithReferenceForeignKeyId").IsNullable);
Assert.True(entityType.GetProperty("OptionalNavigationWithReferenceForeignKeyId").IsNullable);
Assert.False(entityType.GetProperty("RequiredNavigationWithValueForeignKeyId").IsNullable);
Assert.True(entityType.GetProperty("OptionalNavigationWithValueForeignKeyId").IsNullable);
Assert.True(entityType.FindNavigation("RequiredNavigationWithReferenceForeignKey")!.ForeignKey.IsRequired);
Assert.False(entityType.FindNavigation("OptionalNavigationWithReferenceForeignKey")!.ForeignKey.IsRequired);
Assert.True(entityType.FindNavigation("RequiredNavigationWithValueForeignKey")!.ForeignKey.IsRequired);
Expand Down

0 comments on commit ec9c6c2

Please sign in to comment.