Skip to content

Commit

Permalink
Scaffold table and column description as XML comments. (#22937)
Browse files Browse the repository at this point in the history
fixes #19113

Co-authored-by: Erik Ejlskov Jensen <[email protected]>
  • Loading branch information
ErikEJ and Erik Ejlskov Jensen authored Oct 16, 2020
1 parent 5a8784e commit 82232b3
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ protected virtual void GenerateClass([NotNull] IEntityType entityType)
{
Check.NotNull(entityType, nameof(entityType));

GenerateComment(entityType.GetComment());

if (_useDataAnnotations)
{
GenerateEntityTypeDataAnnotations(entityType);
Expand Down Expand Up @@ -264,6 +266,8 @@ protected virtual void GenerateProperties([NotNull] IEntityType entityType)

foreach (var property in entityType.GetProperties().OrderBy(p => p.GetColumnOrdinal()))
{
GenerateComment(property.GetComment());

if (_useDataAnnotations)
{
GeneratePropertyDataAnnotations(property);
Expand Down Expand Up @@ -449,6 +453,21 @@ private void GenerateInversePropertyAttribute(INavigation navigation)
}
}

private void GenerateComment(string comment)
{
if (!string.IsNullOrWhiteSpace(comment))
{
_sb.AppendLine("/// <summary>");

foreach (var line in comment.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None))
{
_sb.AppendLine($"/// {System.Security.SecurityElement.Escape(line)}");
}

_sb.AppendLine("/// </summary>");
}
}

private sealed class AttributeWriter
{
private readonly string _attributeName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,104 @@ public partial class Entity
});
}

[ConditionalFact]
public void Comments_are_generated()
{
Test(
modelBuilder => modelBuilder
.Entity(
"Entity",
x =>
{
x.HasComment("Entity Comment");
x.Property<int>("Id").HasComment("Property Comment");
})
,
new ModelCodeGenerationOptions { UseDataAnnotations = true },
code =>
{
AssertFileContents(
@"using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
#nullable disable
namespace TestNamespace
{
/// <summary>
/// Entity Comment
/// </summary>
public partial class Entity
{
/// <summary>
/// Property Comment
/// </summary>
[Key]
public int Id { get; set; }
}
}
",
code.AdditionalFiles.Single(f => f.Path == "Entity.cs"));
},
model => { });
}

[ConditionalFact]
public void Comments_complex_are_generated()
{
Test(
modelBuilder => modelBuilder
.Entity(
"Entity",
x =>
{
x.HasComment(@"Entity Comment
On multiple lines
With XML content <br/>");
x.Property<int>("Id").HasComment(@"Property Comment
On multiple lines
With XML content <br/>");
})
,
new ModelCodeGenerationOptions { UseDataAnnotations = true },
code =>
{
AssertFileContents(
@"using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
#nullable disable
namespace TestNamespace
{
/// <summary>
/// Entity Comment
/// On multiple lines
/// With XML content &lt;br/&gt;
/// </summary>
public partial class Entity
{
/// <summary>
/// Property Comment
/// On multiple lines
/// With XML content &lt;br/&gt;
/// </summary>
[Key]
public int Id { get; set; }
}
}
",
code.AdditionalFiles.Single(f => f.Path == "Entity.cs"));
},
model => { });
}

[ConditionalFact]
public void Properties_are_sorted_in_order_of_definition_in_table()
{
Expand Down

0 comments on commit 82232b3

Please sign in to comment.