Skip to content

Commit

Permalink
RevEng: Don't use nameof syntax (#26622) (#27231)
Browse files Browse the repository at this point in the history
Scoping causes clash in names.

Resolves #26588
  • Loading branch information
smitpatel authored Feb 2, 2022
1 parent 1f14e85 commit 06e2f6e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 59 deletions.
31 changes: 10 additions & 21 deletions src/EFCore.Design/Scaffolding/Internal/CSharpEntityTypeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ private void GenerateIndexAttributes(IEntityType entityType)
var indexAttribute = new AttributeWriter(nameof(IndexAttribute));
foreach (var property in index.Properties)
{
indexAttribute.AddParameter($"nameof({property.Name})");
// Do NOT use nameof for property.Name
indexAttribute.AddParameter(_code.Literal(property.Name));
}

if (index.Name != null)
Expand Down Expand Up @@ -496,11 +497,8 @@ private void GenerateInversePropertyAttribute(ISkipNavigation navigation)
{
var inversePropertyAttribute = new AttributeWriter(nameof(InversePropertyAttribute));

inversePropertyAttribute.AddParameter(
!navigation.DeclaringEntityType.GetPropertiesAndNavigations().Any(
m => m.Name == inverseNavigation.DeclaringEntityType.Name)
? $"nameof({inverseNavigation.DeclaringEntityType.Name}.{inverseNavigation.Name})"
: _code.Literal(inverseNavigation.Name));
// Do NOT use nameof for inverseNavigation.Name
inversePropertyAttribute.AddParameter(_code.Literal(inverseNavigation.Name));

_sb.AppendLine(inversePropertyAttribute.ToString());
}
Expand Down Expand Up @@ -557,16 +555,10 @@ private void GenerateForeignKeyAttribute(INavigation navigation)
{
var foreignKeyAttribute = new AttributeWriter(nameof(ForeignKeyAttribute));

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})");
}
// Do NOT use nameof syntax
foreignKeyAttribute.AddParameter(
_code.Literal(
string.Join(",", navigation.ForeignKey.Properties.Select(p => p.Name))));

_sb.AppendLine(foreignKeyAttribute.ToString());
}
Expand All @@ -583,11 +575,8 @@ private void GenerateInversePropertyAttribute(INavigation navigation)
{
var inversePropertyAttribute = new AttributeWriter(nameof(InversePropertyAttribute));

inversePropertyAttribute.AddParameter(
!navigation.DeclaringEntityType.GetPropertiesAndNavigations().Any(
m => m.Name == inverseNavigation.DeclaringEntityType.Name)
? $"nameof({inverseNavigation.DeclaringEntityType.Name}.{inverseNavigation.Name})"
: _code.Literal(inverseNavigation.Name));
// Do NOT use nameof for inverseNavigation.Name
inversePropertyAttribute.AddParameter(_code.Literal(inverseNavigation.Name));

_sb.AppendLine(inversePropertyAttribute.ToString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ public void IndexAttribute_is_generated_for_multiple_indexes_with_name_unique()
namespace TestNamespace
{
[Index(nameof(C))]
[Index(nameof(A), nameof(B), Name = ""IndexOnAAndB"", IsUnique = true)]
[Index(nameof(B), nameof(C), Name = ""IndexOnBAndC"")]
[Index(""C"")]
[Index(""A"", ""B"", Name = ""IndexOnAAndB"", IsUnique = true)]
[Index(""B"", ""C"", Name = ""IndexOnBAndC"")]
public partial class EntityWithIndexes
{
[Key]
Expand Down Expand Up @@ -354,7 +354,7 @@ public void Entity_with_indexes_generates_IndexAttribute_only_for_indexes_withou
namespace TestNamespace
{
[Index(nameof(A), nameof(B), Name = ""IndexOnAAndB"", IsUnique = true)]
[Index(""A"", ""B"", Name = ""IndexOnAAndB"", IsUnique = true)]
public partial class EntityWithIndexes
{
[Key]
Expand Down Expand Up @@ -741,17 +741,17 @@ public partial class Entity
public string RequiredReferenceNavigationId { get; set; }
public int RequiredValueNavigationId { get; set; }
[ForeignKey(nameof(OptionalReferenceNavigationId))]
[InverseProperty(nameof(Dependent2.Entity))]
[ForeignKey(""OptionalReferenceNavigationId"")]
[InverseProperty(""Entity"")]
public virtual Dependent2 OptionalReferenceNavigation { get; set; }
[ForeignKey(nameof(OptionalValueNavigationId))]
[InverseProperty(nameof(Dependent4.Entity))]
[ForeignKey(""OptionalValueNavigationId"")]
[InverseProperty(""Entity"")]
public virtual Dependent4 OptionalValueNavigation { get; set; }
[ForeignKey(nameof(RequiredReferenceNavigationId))]
[InverseProperty(nameof(Dependent1.Entity))]
[ForeignKey(""RequiredReferenceNavigationId"")]
[InverseProperty(""Entity"")]
public virtual Dependent1 RequiredReferenceNavigation { get; set; }
[ForeignKey(nameof(RequiredValueNavigationId))]
[InverseProperty(nameof(Dependent3.Entity))]
[ForeignKey(""RequiredValueNavigationId"")]
[InverseProperty(""Entity"")]
public virtual Dependent3 RequiredValueNavigation { get; set; }
}
}
Expand Down Expand Up @@ -815,17 +815,17 @@ public partial class Entity
public string RequiredNavigationWithReferenceForeignKeyId { get; set; } = null!;
public int RequiredNavigationWithValueForeignKeyId { get; set; }
[ForeignKey(nameof(OptionalNavigationWithReferenceForeignKeyId))]
[InverseProperty(nameof(Dependent2.Entity))]
[ForeignKey(""OptionalNavigationWithReferenceForeignKeyId"")]
[InverseProperty(""Entity"")]
public virtual Dependent2? OptionalNavigationWithReferenceForeignKey { get; set; }
[ForeignKey(nameof(OptionalNavigationWithValueForeignKeyId))]
[InverseProperty(nameof(Dependent4.Entity))]
[ForeignKey(""OptionalNavigationWithValueForeignKeyId"")]
[InverseProperty(""Entity"")]
public virtual Dependent4? OptionalNavigationWithValueForeignKey { get; set; }
[ForeignKey(nameof(RequiredNavigationWithReferenceForeignKeyId))]
[InverseProperty(nameof(Dependent1.Entity))]
[ForeignKey(""RequiredNavigationWithReferenceForeignKeyId"")]
[InverseProperty(""Entity"")]
public virtual Dependent1 RequiredNavigationWithReferenceForeignKey { get; set; } = null!;
[ForeignKey(nameof(RequiredNavigationWithValueForeignKeyId))]
[InverseProperty(nameof(Dependent3.Entity))]
[ForeignKey(""RequiredNavigationWithValueForeignKeyId"")]
[InverseProperty(""Entity"")]
public virtual Dependent3 RequiredNavigationWithValueForeignKey { get; set; } = null!;
}
}
Expand Down Expand Up @@ -889,17 +889,17 @@ public partial class Entity
public string RequiredNavigationWithReferenceForeignKeyId { get; set; } = null!;
public int RequiredNavigationWithValueForeignKeyId { get; set; }
[ForeignKey(nameof(OptionalNavigationWithReferenceForeignKeyId))]
[InverseProperty(nameof(Dependent2.Entity))]
[ForeignKey(""OptionalNavigationWithReferenceForeignKeyId"")]
[InverseProperty(""Entity"")]
public virtual Dependent2? OptionalNavigationWithReferenceForeignKey { get; set; }
[ForeignKey(nameof(OptionalNavigationWithValueForeignKeyId))]
[InverseProperty(nameof(Dependent4.Entity))]
[ForeignKey(""OptionalNavigationWithValueForeignKeyId"")]
[InverseProperty(""Entity"")]
public virtual Dependent4? OptionalNavigationWithValueForeignKey { get; set; }
[ForeignKey(nameof(RequiredNavigationWithReferenceForeignKeyId))]
[InverseProperty(nameof(Dependent1.Entity))]
[ForeignKey(""RequiredNavigationWithReferenceForeignKeyId"")]
[InverseProperty(""Entity"")]
public virtual Dependent1 RequiredNavigationWithReferenceForeignKey { get; set; } = null!;
[ForeignKey(nameof(RequiredNavigationWithValueForeignKeyId))]
[InverseProperty(nameof(Dependent3.Entity))]
[ForeignKey(""RequiredNavigationWithValueForeignKeyId"")]
[InverseProperty(""Entity"")]
public virtual Dependent3 RequiredNavigationWithValueForeignKey { get; set; } = null!;
}
}
Expand Down Expand Up @@ -1401,8 +1401,8 @@ public Post()
public int Id { get; set; }
public int? AuthorId { get; set; }
[ForeignKey(nameof(AuthorId))]
[InverseProperty(nameof(Person.Posts))]
[ForeignKey(""AuthorId"")]
[InverseProperty(""Posts"")]
public virtual Person Author { get; set; }
public virtual ICollection<Contribution> Contributions { get; set; }
}
Expand All @@ -1429,7 +1429,7 @@ public Person()
[Key]
public int Id { get; set; }
[InverseProperty(nameof(Post.Author))]
[InverseProperty(""Author"")]
public virtual ICollection<Post> Posts { get; set; }
}
}
Expand Down Expand Up @@ -1490,7 +1490,7 @@ public partial class Post
public int? BlogId2 { get; set; }
[ForeignKey(""BlogId1,BlogId2"")]
[InverseProperty(nameof(Blog.Posts))]
[InverseProperty(""Posts"")]
public virtual Blog BlogNavigation { get; set; }
}
}
Expand Down Expand Up @@ -1710,7 +1710,7 @@ public partial class Post
public int Id { get; set; }
public int? BlogId { get; set; }
[ForeignKey(nameof(BlogId))]
[ForeignKey(""BlogId"")]
[InverseProperty(""Posts"")]
public virtual Blog Blog { get; set; }
}
Expand Down Expand Up @@ -1765,7 +1765,7 @@ public partial class Post
public int Id { get; set; }
public int? Blog { get; set; }
[ForeignKey(nameof(Blog))]
[ForeignKey(""Blog"")]
[InverseProperty(""Posts"")]
public virtual Blog BlogNavigation { get; set; }
}
Expand Down Expand Up @@ -1822,10 +1822,10 @@ public partial class Post
public int? BlogId { get; set; }
public int? OriginalBlogId { get; set; }
[ForeignKey(nameof(BlogId))]
[ForeignKey(""BlogId"")]
[InverseProperty(""Posts"")]
public virtual Blog Blog { get; set; }
[ForeignKey(nameof(OriginalBlogId))]
[ForeignKey(""OriginalBlogId"")]
[InverseProperty(""OriginalPosts"")]
public virtual Blog OriginalBlog { get; set; }
}
Expand Down Expand Up @@ -2430,7 +2430,7 @@ public Blog()
public int Id { get; set; }
[ForeignKey(""BlogsId"")]
[InverseProperty(nameof(Post.Blogs))]
[InverseProperty(""Blogs"")]
public virtual ICollection<Post> Posts { get; set; }
}
}
Expand All @@ -2456,7 +2456,7 @@ public Post()
public int Id { get; set; }
[ForeignKey(""PostsId"")]
[InverseProperty(nameof(Blog.Posts))]
[InverseProperty(""Posts"")]
public virtual ICollection<Blog> Blogs { get; set; }
}
}
Expand Down

0 comments on commit 06e2f6e

Please sign in to comment.