Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core 6 Decimal precision warning

After upgrading to EF Core 6, I have this annoying warning here when adding a migration:

No store type was specified for the decimal property '{property}' on entity type '{entityType}'. This will cause values to be silently truncated if they do not fit in the default precision and scale. Explicitly specify the SQL server column type that can accommodate all the values in 'OnModelCreating' using 'HasColumnType', specify precision and scale using 'HasPrecision', or configure a value converter using 'HasConversion'.

which is defined here: https://github.com/dotnet/efcore/blob/main/src/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs

I am not sure to understand the purpose of this warning because on entities with the tag [Keyless] as they do not live in the database. Also adding an attribute such as [Column(TypeName = "decimal(28, 6)")] doesn't seem to clear the warning on keyless entities.

like image 314
sofsntp Avatar asked Sep 10 '25 18:09

sofsntp


2 Answers

Apparently there's a new attribute for EF Core 6:

using Microsoft.EntityFrameworkCore;

...

[Precision(18, 2)]
like image 113
Ryan Naccarato Avatar answered Sep 13 '25 13:09

Ryan Naccarato


In your IEntityTypeConfiguration implementation you can also use .HasPrecision(int precision, int scale)

public class YourTypeConfiguration : IEntityTypeConfiguration<YourType> {
    public void Configure(EntityTypeBuilder<YourType> builder) {
        builder.ToTable("TableName");

        builder.Property(i => i.YourProperty).HasPrecision(28,6); 
    }
}
like image 41
Jasper Risseeuw Avatar answered Sep 13 '25 14:09

Jasper Risseeuw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!