diff --git a/src/EFCore.PG/Design/Internal/NpgsqlAnnotationCodeGenerator.cs b/src/EFCore.PG/Design/Internal/NpgsqlAnnotationCodeGenerator.cs index 2ac672cf8..3ba3be58f 100644 --- a/src/EFCore.PG/Design/Internal/NpgsqlAnnotationCodeGenerator.cs +++ b/src/EFCore.PG/Design/Internal/NpgsqlAnnotationCodeGenerator.cs @@ -305,6 +305,19 @@ public override IReadOnlyList GenerateFluentApiCalls( IProperty property, IDictionary annotations) { + // For precision-only numeric properties, the snapshot's inferred numeric(p) store type is parsed with scale 0, + // producing numeric(p,0) rather than the original numeric(p) and triggering false pending model changes. + // Omit the inferred column type so HasPrecision(p) reconstructs the original mapping, while preserving + // explicitly configured column types. See https://github.com/npgsql/efcore.pg/issues/3521. + if (property.GetPrecision() is { } precision + && property.GetScale() is null + && property.FindAnnotation(RelationalAnnotationNames.ColumnType) is null + && annotations.TryGetValue(RelationalAnnotationNames.ColumnType, out var columnType) + && (string?)columnType.Value == $"numeric({precision})") + { + annotations.Remove(RelationalAnnotationNames.ColumnType); + } + var fragments = new List(base.GenerateFluentApiCalls(property, annotations)); if (GenerateValueGenerationStrategy(annotations, property.DeclaringType.Model, onModel: false) is { } valueGenerationStrategy) diff --git a/test/EFCore.PG.Tests/Design/Internal/NpgsqlAnnotationCodeGeneratorTest.cs b/test/EFCore.PG.Tests/Design/Internal/NpgsqlAnnotationCodeGeneratorTest.cs index b3678074b..2c8c907fc 100644 --- a/test/EFCore.PG.Tests/Design/Internal/NpgsqlAnnotationCodeGeneratorTest.cs +++ b/test/EFCore.PG.Tests/Design/Internal/NpgsqlAnnotationCodeGeneratorTest.cs @@ -9,6 +9,38 @@ namespace Npgsql.EntityFrameworkCore.PostgreSQL.Design.Internal; public class NpgsqlAnnotationCodeGeneratorTest { + [Theory] + [InlineData(null, false, "numeric(9)", null)] + [InlineData(0, false, "numeric(9,0)", "numeric(9,0)")] + [InlineData(2, false, "numeric(9,2)", "numeric(9,2)")] + [InlineData(null, true, "numeric", "numeric")] + [InlineData(null, true, "numeric(9)", "numeric(9)")] + [InlineData(null, true, "numeric(9,2)", "numeric(9,2)")] + public void GenerateFluentApi_numeric_precision(int? scale, bool explicitlyConfigured, string storeType, string expectedStoreType) + { + var generator = CreateGenerator(); + var modelBuilder = new ModelBuilder(NpgsqlConventionSetBuilder.Build()); + var propertyBuilder = modelBuilder.Entity("Parameter").Property("ScalingFactor").HasPrecision(9); + if (scale is { } s) + { + propertyBuilder.HasPrecision(9, s); + } + + if (explicitlyConfigured) + { + propertyBuilder.HasColumnType(storeType); + } + + var property = (IProperty)propertyBuilder.Metadata; + var annotations = property.GetAnnotations().ToDictionary(a => a.Name, a => a); + annotations.TryAdd(RelationalAnnotationNames.ColumnType, new Annotation(RelationalAnnotationNames.ColumnType, storeType)); + + var result = generator.GenerateFluentApiCalls(property, annotations) + .SingleOrDefault(c => c.Method == nameof(RelationalPropertyBuilderExtensions.HasColumnType)); + + Assert.Equal(expectedStoreType, result?.Arguments.Single()); + } + #region Identity / sequence / HiLo [Fact]