From c31ec2a064119deca00617eb719f1060b7075be2 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Thu, 25 Jul 2019 23:10:01 +0200 Subject: [PATCH] Apply column comments when creating tables --- .../Migrations/MigrationsSqlGenerator.cs | 5 +++++ .../MigrationSqlGeneratorTestBase.cs | 6 ++++-- .../SqlServerMigrationSqlGeneratorTest.cs | 21 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs b/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs index d0419e3ad2f..aefe3b6ca3d 100644 --- a/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs +++ b/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs @@ -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); diff --git a/test/EFCore.Relational.Specification.Tests/MigrationSqlGeneratorTestBase.cs b/test/EFCore.Relational.Specification.Tests/MigrationSqlGeneratorTestBase.cs index 23fb2c284ca..2dc8158bf13 100644 --- a/test/EFCore.Relational.Specification.Tests/MigrationSqlGeneratorTestBase.cs +++ b/test/EFCore.Relational.Specification.Tests/MigrationSqlGeneratorTestBase.cs @@ -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 { @@ -534,7 +535,8 @@ public virtual void CreateTableOperation() PrincipalTable = "Companies", PrincipalColumns = new[] { "Id" } } - } + }, + Comment = "Table comment" }); [ConditionalFact] diff --git a/test/EFCore.SqlServer.FunctionalTests/SqlServerMigrationSqlGeneratorTest.cs b/test/EFCore.SqlServer.FunctionalTests/SqlServerMigrationSqlGeneratorTest.cs index 070f83269d5..33ddde09f89 100644 --- a/test/EFCore.SqlServer.FunctionalTests/SqlServerMigrationSqlGeneratorTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/SqlServerMigrationSqlGeneratorTest.cs @@ -17,6 +17,27 @@ 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();