Skip to content

Commit

Permalink
Apply column comments when creating tables
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Jul 25, 2019
1 parent c39853f commit 1fd4a5a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,11 @@ protected virtual void Generate(
GenerateComment(operation, model, builder, operation.Comment, operation.Schema, operation.Name);
}

foreach (var column in operation.Columns.Where(c => c.Comment != null))
{
GenerateComment(operation, model, builder, column.Comment, operation.Schema, operation.Name, column.Name);
}

if (terminate)
{
builder.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,8 @@ public virtual void CreateTableOperation()
Name = "EmployerId",
Table = "People",
ClrType = typeof(int),
IsNullable = true
IsNullable = true,
Comment = "Employer ID comment"
},
new AddColumnOperation
{
Expand Down Expand Up @@ -534,7 +535,8 @@ public virtual void CreateTableOperation()
PrincipalTable = "Companies",
PrincipalColumns = new[] { "Id" }
}
}
},
Comment = "Table comment"
});

[ConditionalFact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@ namespace Microsoft.EntityFrameworkCore
{
public class SqlServerMigrationSqlGeneratorTest : MigrationSqlGeneratorTestBase
{
public override void CreateTableOperation()
{
base.CreateTableOperation();

Assert.Equal(
@"CREATE TABLE [dbo].[People] (
[Id] int NOT NULL,
[EmployerId] int NULL,
[SSN] char(11) NULL,
PRIMARY KEY ([Id]),
UNIQUE ([SSN]),
CHECK (SSN > 0),
FOREIGN KEY ([EmployerId]) REFERENCES [Companies] ([Id])
)GO
EXEC sp_addextendedproperty @name = N'Comment', @value = N'Table comment', @level0type = N'Schema', @level0name = N'dbo', @level1type = N'Table', @level1name = N'People'GO
EXEC sp_addextendedproperty @name = N'Comment', @value = N'Employer ID comment', @level0type = N'Schema', @level0name = N'dbo', @level1type = N'Table', @level1name = N'People', @level2type = N'Column', @level2name = N'EmployerId';
",
Sql, ignoreLineEndingDifferences: true);
}

public override void CreateIndexOperation_with_filter_where_clause()
{
base.CreateIndexOperation_with_filter_where_clause();
Expand Down Expand Up @@ -850,7 +872,7 @@ public void AlterColumnOperation_with_new_comment()
+ EOL +
"WHERE ([d].[parent_object_id] = OBJECT_ID(N'[dbo].[People]') AND [c].[name] = N'LuckyNumber');" + EOL +
"IF @var0 IS NOT NULL EXEC(N'ALTER TABLE [dbo].[People] DROP CONSTRAINT [' + @var0 + '];');" + EOL +
"ALTER TABLE [dbo].[People] ALTER COLUMN [LuckyNumber] int NOT NULL;" + EOL +
"ALTER TABLE [dbo].[People] ALTER COLUMN [LuckyNumber] int NOT NULL;" + EOL +
"GO" + EOL + EOL +
"EXEC sp_addextendedproperty @name = N'Comment', @value = N'My Comment', @level0type = N'Schema', @level0name = N'dbo', @level1type = N'Table', @level1name = N'People', @level2type = N'Column', @level2name = N'LuckyNumber'",
Sql);
Expand Down

0 comments on commit 1fd4a5a

Please sign in to comment.