sofsntp
sofsntp

Reputation: 2164

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.

Upvotes: 24

Views: 14714

Answers (2)

Jasper Risseeuw
Jasper Risseeuw

Reputation: 1256

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

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

        builder.Property(i => i.YourProperty).HasPrecision(28,6); 
    }
}

Upvotes: 2

Ryan Naccarato
Ryan Naccarato

Reputation: 1211

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

using Microsoft.EntityFrameworkCore;

...

[Precision(18, 2)]

Upvotes: 33

Related Questions