Skip to content

Scaffolding fails to generate comments for Views (Entity and Configuration) #3934

Closed as duplicate

Description

@OlegUfaev

When reverse engineering (scaffolding) a PostgreSQL database using dotnet ef dbcontext scaffold, comments applied to Views (COMMENT ON VIEW) are completely omitted from the generated code.

While column-level comments for views (COMMENT ON COLUMN) are successfully scaffolded as both XML documentation tags and .HasComment() fluent API calls, the view-level comments themselves are missing entirely from both the entity partial class and the OnModelCreating configuration. This issue specifically affects Views, whereas database Tables have their comments scaffolded correctly.

Steps to Reproduce

  1. Create a test database in PostgreSQL and execute the following SQL script to create a view with comments:
CREATE OR REPLACE VIEW public.test_view AS 
SELECT 1 AS f1;

COMMENT ON VIEW public.test_view IS 'View description';
COMMENT ON COLUMN public.test_view.f1 IS 'Field description';
  1. Run the EF Core scaffolding command:
dotnet ef dbcontext scaffold "Host=localhost; Port=5432; Database=test; Username=postgres; Password=xxx" Npgsql.EntityFrameworkCore.PostgreSQL --schema public --no-onconfiguring --force

Actual Behavior

  1. Generated Entity Class (TestView.cs): Contains XML documentation for the property, but lacks XML documentation for the class itself.
using System;
using System.Collections.Generic;

namespace Scaffolding.EF.Postgres;

// Missing XML documentation comment here ('View description')
public partial class TestView
{
    /// <summary>
    /// Field description
    /// </summary>
    public int? F1 { get; set; }
}
  1. Generated DB Context (TestDbContext.cs): Column configuration includes .HasComment(), but the view mapping lacks .HasComment().
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<TestView>(entity =>
    {
        entity
            .HasNoKey()
            .ToView("test_view"); // Missing .HasComment("View description") here

        entity.Property(e => e.F1)
            .HasComment("Field description")
            .HasColumnName("f1");
    });

    OnModelCreatingPartial(modelBuilder);
}

Expected Behavior

The scaffolded entity class should include an XML summary tag containing the view description, and the fluent API configuration should include .HasComment("View description") chained to .ToView(), similar to how regular tables are processed:

/// <summary>
/// View description
/// </summary>
public partial class TestView { ... }
entity
    .HasNoKey()
    .ToView("test_view")
    .HasComment("View description");

Environment / Technical Details

• PostgreSQL Version: 15.17
• Microsoft.EntityFrameworkCore.Design: 10.0.12
• Npgsql.EntityFrameworkCore.PostgreSQL: 10.0.3
• Target Framework: .NET 10 / ASP.NET Core 10

Activity

  1. added theissue type on Oct 15, 2026
  2. roji commented on Oct 15, 2026

    @roji
    Member

    AI Triage

    The below is an AI-generated analysis and may contain inaccuracies.

    Confirmed. The comment is lost in EF Core's shared scaffolding pipeline, not in PostgreSQL metadata extraction. Classified this issue as Bug.

    Reproduction and regression status

    Executed a standalone console reproduction against PostgreSQL 18.4, using the SQL from this report and a commented table as a control:

    Npgsql provider EF Core Design Result
    9.0.4 9.0.12 View description omitted
    10.0.2 10.0.11 View description omitted
    10.0.3 10.0.12 View description omitted
    10.0.3 10.0.13 View description omitted

    In every case, both the class XML summary and view-level fluent configuration are missing, while column comments and the control table's comments are preserved.

    This is not newly introduced by Npgsql 10.0.3 or EF Core 10: the previous patch combination and EF Core 9 also reproduce it. No earlier working version was identified, so an original regression boundary is not established. Updating to EF Core 10.0.13 does not resolve it.

    The same metadata-to-generated-code failure also reproduces with SQL Server design-time services on EF Core 9.0.12, 10.0.12 and 10.0.13. That comparison uses an in-memory DatabaseView containing the comment; it does not connect to a SQL Server database. The executable repro below isolates that shared pipeline without requiring either database server.

    Cause and routing

    Npgsql correctly populates DatabaseView.Comment with "View description"; this was verified during execution and matches the provider's metadata extraction code.

    In EF Core's RelationalScaffoldingModelFactory.VisitTable, the DatabaseView branch only calls ToView(...). Applying table.Comment occurs exclusively in the non-view branch. Consequently, the scaffolded entity's GetComment() is null. The entity template then has no comment to emit as XML documentation. The same view/table branching is still present in current EF Core source.

    Recommendation: close this provider issue in favor of reopening/tracking it in dotnet/efcore, with the repro below. This is not PostgreSQL-specific.

    Duplicate search

    No exact duplicate was found among open or closed EF Core issues. Related, but not duplicates:

    • dotnet/efcore#19113: general scaffolding of XML documentation from database comments.
    • dotnet/efcore#33051: SQL Server descriptions becoming XML documentation; closed after confirming ordinary scaffolding already handles them. It does not address the view-specific loss here.
    minimal repro

    Create a .NET 10 console project with these package references, replace Program.cs with the following, and run dotnet run. No SQL Server instance is needed: the connection string is never opened, and the commented view metadata is supplied directly to the scaffolding model factory.

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net10.0</TargetFramework>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.12" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.12" />
      </ItemGroup>
    </Project>
    using System;
    using System.Linq;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Design;
    using Microsoft.EntityFrameworkCore.Scaffolding;
    using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
    using Microsoft.EntityFrameworkCore.SqlServer.Design.Internal;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    
    using var context = new TestContext();
    var services = new ServiceCollection();
    services.AddDbContextDesignTimeServices(context);
    services.AddEntityFrameworkDesignTimeServices();
    new SqlServerDesignTimeServices().ConfigureDesignTimeServices(services);
    using var provider = services.BuildServiceProvider();
    
    var database = new DatabaseModel { DatabaseName = "Repro", DefaultSchema = "dbo" };
    var view = new DatabaseView
    {
        Database = database,
        Schema = "dbo",
        Name = "test_view",
        Comment = "View description"
    };
    view.Columns.Add(new DatabaseColumn
    {
        Table = view,
        Name = "f1",
        StoreType = "int",
        IsNullable = true,
        Comment = "Field description"
    });
    database.Tables.Add(view);
    
    var model = provider.GetRequiredService<IScaffoldingModelFactory>()
        .Create(database, new ModelReverseEngineerOptions());
    var options = new ModelCodeGenerationOptions
    {
        ContextName = "TestDbContext",
        ModelNamespace = "Repro",
        ConnectionString = context.Database.GetConnectionString(),
        SuppressOnConfiguring = true
    };
    var code = provider.GetRequiredService<IModelCodeGeneratorSelector>()
        .Select(options).GenerateModel(model, options);
    Console.WriteLine(code.AdditionalFiles.Single().Code);
    Console.WriteLine(code.ContextFile.Code);
    
    public class TestContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseSqlServer("Server=unused;Database=Repro;Integrated Security=true")
                .LogTo(Console.WriteLine, LogLevel.Information)
                .EnableSensitiveDataLogging();
    }

    The generated entity has a property XML summary containing Field description, and its context contains .HasComment("Field description"). Neither generated file contains View description, despite that value being present on the input DatabaseView.

  3. roji commented on Oct 15, 2026

    @roji
    Member

    Duplicate of dotnet/efcore#39264

    The shared EF Core scaffolding pipeline drops the view comment; the upstream issue includes a minimal reproduction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions