Adding comments to SQL code generated by Entity Framework Core
A funny feature that I've encountered recently. It's not something most people would find useful, but it helps tremendously with tracing and debugging what is going on. It's easy, just add .TagWith(someString) to your LINQ query and it will generate comments in SQL. More details here: Query tags.
Comments
You can use [EFCommenter](https://www.nuget.org/packages/EFCommenter/#versions-body-tab). All summaries of entities, properties, and enums will be added as comments on the corresponding database tables and columns. * Automatically adds **table comments** from entity class summaries. * Adds **column comments** from property summaries. * Supports **enum types** as column comments. * Works with **SQL Server** and **PostgreSQL**. * Easy to integrate into your EF Core project using a single extension method. ## Installation ```bash dotnet add package EFCommenter ``` ## Usage Enable XML documentation in your project by adding this to your `.csproj`: ```xml <PropertyGroup> <GenerateDocumentationFile>true</GenerateDocumentationFile> </PropertyGroup> ``` Then, call the extension method in your `DbContext`: ```csharp using EFCommenter; using Microsoft.EntityFrameworkCore; protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.AddEntitiesComments(); } ``` --- ## Example Your entities and enums include some comments. ```csharp /// <summary> /// Represents a person entity. /// </summary> public class Person { public int Id { get; set; } /// <summary> /// The full name of the person. /// </summary> public string Name { get; set; } = ""; public PersonType Type { get; set; } } public enum PersonType { Admin, User, Guest } ``` After add migrations, the summaries will appear as table and column comments in created migration, and ready to update the database comments. ```csharp migrationBuilder.CreateTable( name: "People", columns: table => new { Id = table.Column<int>(type: "int", nullable: false) .Annotation("SqlServer:Identity", "1, 1"), Name = table.Column<string>(type: "nvarchar(max)", nullable: false, comment: "The full name of the person!!!"), Type = table.Column<int>(type: "int", nullable: false, comment: "0: Admin | \n1: User | \n2: Guest | ") }, constraints: table => { table.PrimaryKey("PK_People", x => x.Id); }, comment: "The class declered a person!!!"); ``` --- ## Notes * Make sure your EF Core project targets **.NET 8** or later. * XML documentation file must be enabled for the summaries to be read. * Tested with **PostgreSQL** and **SQL Server**.
Roohi