Skip to content

PrecisionAttribute on Decimal parameter causes false positive for PendingModelChangesWarning exception #3521

Closed

Description

@Luk164

Adding PrecisionAtribute to a Decimal parameter causes false positive for PendingModelChangesWarning exception.

Example:

[Precision(9)]
public decimal? ScalingFactor { get; set; }

Creating a migration works normally, but trying to apply it will result in PendingModelChangesWarning. Creating new migration afterwards results in an AlterColumn call for ScalingFactor that does not actually change anything.

Result of 1st migration:

public partial class ScaleFactorAddedAgain : Migration
{
    /// <inheritdoc />
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterDatabase()
            .OldAnnotation("Npgsql:PostgresExtension:pg_trgm", ",,");

        migrationBuilder.AddColumn<decimal>(
            name: "scaling_factor",
            table: "parameter",
            type: "numeric(9)",
            precision: 9,
            nullable: true);
    }

    /// <inheritdoc />
    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropColumn(
            name: "scaling_factor",
            table: "parameter");

        migrationBuilder.AlterDatabase()
            .Annotation("Npgsql:PostgresExtension:pg_trgm", ",,");
    }
}

Result of a subsequent migration (no other changes made to model):

    public partial class ScaleFactorAddedAgain2 : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterColumn<decimal>(
                name: "scaling_factor",
                table: "parameter",
                type: "numeric(9)",
                precision: 9,
                nullable: true,
                oldClrType: typeof(decimal),
                oldType: "numeric(9,0)",
                oldPrecision: 9,
                oldNullable: true);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterColumn<decimal>(
                name: "scaling_factor",
                table: "parameter",
                type: "numeric(9,0)",
                precision: 9,
                nullable: true,
                oldClrType: typeof(decimal),
                oldType: "numeric(9)",
                oldPrecision: 9,
                oldNullable: true);
        }
    }

Commenting out the annotation, creating a new migration and setting the precision manually works as expected.

Activity

  1. self-assigned this
    on Apr 14, 2025
  2. added theissue type on Apr 14, 2025
  3. added this to the milestone on Apr 14, 2025
  4. roji commented on Apr 14, 2025

    @roji
    Member

    Thanks, I can see this happening.

    As a workaround, simply specify the scale as well:

    [Precision(9, 0)]
    public decimal? ScalingFactor { get; set; }
  5. Luk164 commented on Apr 17, 2025

    @Luk164
    Author

    Thanks, I can see this happening.

    As a workaround, simply specify the scale as well:

    [Precision(9, 0)]
    public decimal? ScalingFactor { get; set; }

    Thanks, that worked, and it also made me realize that I swapped meanings for precision and scale. I thought precision was for decimal count

  6. roji commented on Oct 21, 2026

    @roji
    Member

    AI Triage

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

    Confirmed a PostgreSQL-provider bug on current main (3931c57), and on released Npgsql EF providers 10.0.3, 9.0.4 and 8.0.11. A model containing [Precision(9)] on decimal? ScalingFactor differs from its unchanged migration snapshot: the snapshot resolves to numeric(9,0), while the current model resolves to numeric(9). HasPendingModelChanges() returns true, and the model differ produces the spurious AlterColumnOperation shown in the report. No database connection is needed to reproduce this.

    This is not a newly introduced EF 9 regression: the false model difference also occurs with EF/provider 8.0.11. EF 9's pending-model-change exception makes the existing problem more visible. I have not established an earlier working release.

    The inconsistency is in Npgsql's numeric mapping round trip: [Precision(9)] produces numeric(9) with no configured scale; parsing that store type from the snapshot supplies PostgreSQL's implicit scale of zero, and the mapping subsequently renders numeric(9,0). These are equivalent PostgreSQL types. The fix should make that representation consistent without changing explicit scales or turning unconstrained numeric into a precision-constrained type.

    The equivalent SQL Server 10.0.3 model, compared with its provider-specific snapshot (decimal(9,2)), reports no pending changes. The PostgreSQL [Precision(9, 0)] workaround is consistent with the successful explicit-scale regression cases.

    No duplicate implementing PR or existing AI Triage comment was found. Upstream dotnet/efcore#35285 explains the EF 9 warning change, but does not fix this provider-specific mismatch. dotnet/efcore#29600 concerns precedence of explicit store-type facets, not this snapshot churn; preserving that precedence is important here.

    minimal repro

    Console program targeting .NET 10 with Npgsql.EntityFrameworkCore.PostgreSQL 10.0.3. The snapshot below represents the unchanged precision-only model. Expected output: False; actual output: True.

    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Infrastructure;
    using Microsoft.EntityFrameworkCore.Migrations;
    using Microsoft.Extensions.Logging;
    
    using var context = new TestContext();
    Console.WriteLine(context.Database.HasPendingModelChanges());
    
    public class TestContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder.UseNpgsql()
                .LogTo(Console.WriteLine, LogLevel.Information)
                .EnableSensitiveDataLogging();
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
            => modelBuilder.Entity<Parameter>().Property(e => e.Id).ValueGeneratedNever();
    }
    
    public class Parameter
    {
        public int Id { get; set; }
    
        [Precision(9)]
        public decimal? ScalingFactor { get; set; }
    }
    
    [DbContext(typeof(TestContext))]
    public class TestContextModelSnapshot : ModelSnapshot
    {
        protected override void BuildModel(ModelBuilder modelBuilder)
        {
            modelBuilder.HasAnnotation("Relational:MaxIdentifierLength", 63);
            modelBuilder.UseIdentityByDefaultColumns();
            modelBuilder.Entity("Parameter", b =>
            {
                b.Property<int>("Id").HasColumnType("integer");
                b.Property<decimal?>("ScalingFactor").HasPrecision(9).HasColumnType("numeric(9)");
                b.HasKey("Id");
                b.ToTable("Parameter");
            });
        }
    }
  7. modified the milestones: , 10.0.4 on Oct 21, 2026
  8. added 3 commits that reference this issue on Oct 21, 2026
    c68efd0
    52d23cd
    258437c
  9. added a commit that references this issue on Oct 21, 2026
    6e86acd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions