diff --git a/src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs b/src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs index e80cd9f078d..c72259e92bc 100644 --- a/src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs +++ b/src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs @@ -11,7 +11,6 @@ using Microsoft.EntityFrameworkCore.Design.Internal; using Microsoft.EntityFrameworkCore.Internal; using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Metadata.Conventions; using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.EntityFrameworkCore.Utilities; @@ -364,7 +363,10 @@ private void GenerateInversePropertyAttribute(INavigation navigation) { var inversePropertyAttribute = new AttributeWriter(nameof(InversePropertyAttribute)); - inversePropertyAttribute.AddParameter($"nameof({inverseNavigation.DeclaringEntityType.Name}.{inverseNavigation.Name})"); + inversePropertyAttribute.AddParameter( + navigation.Name != inverseNavigation.DeclaringEntityType.Name + ? $"nameof({inverseNavigation.DeclaringEntityType.Name}.{inverseNavigation.Name})" + : _code.Literal(inverseNavigation.Name)); _sb.AppendLine(inversePropertyAttribute.ToString()); } diff --git a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs index 77580e83156..11b3442d3d5 100644 --- a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs +++ b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs @@ -76,6 +76,61 @@ public Post() }); } + [ConditionalFact] + public void Navigation_property_with_same_type_and_property_name() + { + Test( + modelBuilder => modelBuilder + .Entity( + "Blog", + x => x.Property("Id")) + .Entity( + "Post", + x => + { + x.Property("Id"); + x.HasOne("Blog", "Blog").WithMany("Posts"); + }), + new ModelCodeGenerationOptions { UseDataAnnotations = true }, + code => + { + var postFile = code.AdditionalFiles.First(f => f.Path == "Post.cs"); + Assert.Equal( + @"using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace TestNamespace +{ + public partial class Post + { + [Key] + public int Id { get; set; } + public int? BlogId { get; set; } + + [ForeignKey(nameof(BlogId))] + [InverseProperty(""Posts"")] + public virtual Blog Blog { get; set; } + } +} +", + postFile.Code); + }, + model => + { + var postType = model.FindEntityType("TestNamespace.Post"); + var blogNavigation = postType.FindNavigation("Blog"); + + var foreignKeyProperty = Assert.Single(blogNavigation.ForeignKey.Properties); + Assert.Equal("BlogId", foreignKeyProperty.Name); + + var inverseNavigation = blogNavigation.FindInverse(); + Assert.Equal("TestNamespace.Blog", inverseNavigation.DeclaringEntityType.Name); + Assert.Equal("Posts", inverseNavigation.Name); + }); + } + [ConditionalFact] public void Composite_key() {