diff --git a/src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs b/src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs index 0487bdb1e99..c62c901822b 100644 --- a/src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs +++ b/src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs @@ -350,9 +350,16 @@ private void GenerateForeignKeyAttribute(INavigation navigation) { var foreignKeyAttribute = new AttributeWriter(nameof(ForeignKeyAttribute)); - foreignKeyAttribute.AddParameter( - _code.Literal( - string.Join(",", navigation.ForeignKey.Properties.Select(p => p.Name)))); + if (navigation.ForeignKey.Properties.Count > 1) + { + foreignKeyAttribute.AddParameter( + _code.Literal( + string.Join(",", navigation.ForeignKey.Properties.Select(p => p.Name)))); + } + else + { + foreignKeyAttribute.AddParameter($"nameof({navigation.ForeignKey.Properties.First().Name})"); + } _sb.AppendLine(foreignKeyAttribute.ToString()); } @@ -369,7 +376,7 @@ private void GenerateInversePropertyAttribute(INavigation navigation) { var inversePropertyAttribute = new AttributeWriter(nameof(InversePropertyAttribute)); - inversePropertyAttribute.AddParameter(_code.Literal(inverseNavigation.Name)); + inversePropertyAttribute.AddParameter($"nameof({inverseNavigation.DeclaringEntityType.Name}.{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 a97f2000459..a45d77bd613 100644 --- a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs +++ b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpEntityTypeGeneratorTest.cs @@ -25,7 +25,7 @@ public void Navigation_properties() { x.Property("Id"); - x.HasOne("Person", "Author").WithMany(); + x.HasOne("Person", "Author").WithMany("Posts"); x.HasMany("Contribution", "Contributions").WithOne(); }), new ModelCodeGenerationOptions @@ -53,7 +53,8 @@ public Post() public int Id { get; set; } public int? AuthorId { get; set; } - [ForeignKey(""AuthorId"")] + [ForeignKey(nameof(AuthorId))] + [InverseProperty(nameof(Person.Posts))] public virtual Person Author { get; set; } public virtual ICollection Contributions { get; set; } }