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

Fix scaffolding of reference navigations with NRT (#27616) #28382

Merged
merged 1 commit into from
Aug 3, 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 @@ -462,7 +462,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 @@ -785,10 +785,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 @@ -810,23 +827,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 @@ -836,11 +845,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