diff --git a/src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs b/src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs index dfd58acb37d..88d0236e569 100644 --- a/src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs +++ b/src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs @@ -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; }}"); } diff --git a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs index b14b813c5e0..b4d6c0afd5f 100644 --- a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs +++ b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs @@ -752,10 +752,27 @@ public void Required_and_not_required_reference_navigations_with_nrt() { x.Property("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("Id")) .Entity("Dependent2", x => x.Property("Id")) @@ -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; } } } ", @@ -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);