15

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.

1
  • It would be easier for others to understand your problem if you share your code. Commented Dec 27, 2019 at 13:32

2 Answers 2

15

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hello, I tried your suggestion, but an error occurs while creating as migration "Object reference not set to an instance of an object."
Hi, I've tested it with EF Core 3.1, SqlServer and it worked for me.
It works only after the table creation is finished, to add comments without the existing table in the database of this error, thanks so much for the help
9

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.