Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/EFCore.PG/Design/Internal/NpgsqlAnnotationCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,19 @@ public override IReadOnlyList<MethodCallCodeFragment> GenerateFluentApiCalls(
IProperty property,
IDictionary<string, IAnnotation> 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<MethodCallCodeFragment>(base.GenerateFluentApiCalls(property, annotations));

if (GenerateValueGenerationStrategy(annotations, property.DeclaringType.Model, onModel: false) is { } valueGenerationStrategy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,38 @@ public void Database_encoding_is_preserved_as_an_annotation()
Assert.Equal("UTF8", annotations[NpgsqlAnnotationNames.DatabaseEncoding].Value);
}

[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<decimal?>("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]
Expand Down
Loading