Joyce Couraça
Joyce Couraça

Reputation: 161

How to add description to columns in Entity Framework Core code-first using migrations?

I am using Entity Framework Core 3.0 code. How do I add descriptions for columns in entity configuration classes or migrations so that they end up as a column description in SQL Server?

There is a publication for Entity Framework 4.3.1 but I could not do it in the Entity Framework Core.

Upvotes: 15

Views: 8274

Answers (2)

Murad
Murad

Reputation: 387

I'm used CommentAttribute.

Data Annotations (EF Core 5.0 and above)

public class Box
{   
    [Comment("Width in centimeters")]
    public string Width { get; set; }
}

Fluent API

modelBuilder.Entity<Box>()
            .Property(b => b.Width)
            .HasComment("Width in centimeters");

Generated SQL script.

COMMENT ON COLUMN shipping.box.width IS 'Width in centimeters';

My stack EF Core 6.0 and PostgreSQL 14.2.

Microsoft documentation

Upvotes: 9

Ivan Stoev
Ivan Stoev

Reputation: 205849

You can use HasComment fluent API:

modelBuilder.Entity<MyEntity>()
    .Property(e => e.MyProperty)
    .HasComment("My Column Description");

For SqlServer this is mapped to the corresponding table column description.

Upvotes: 15

Related Questions