Skip to content

Commit

Permalink
Added nameof syntax in scaffolding data annotations attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Muppets authored and bricelam committed May 10, 2019
1 parent 469177a commit 1cb4021
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void Navigation_properties()
{
x.Property<int>("Id");

x.HasOne("Person", "Author").WithMany();
x.HasOne("Person", "Author").WithMany("Posts");
x.HasMany("Contribution", "Contributions").WithOne();
}),
new ModelCodeGenerationOptions
Expand Down Expand Up @@ -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<Contribution> Contributions { get; set; }
}
Expand Down

0 comments on commit 1cb4021

Please sign in to comment.