EF Core 6 十进制精度警告

15

升级到EF Core 6后,我在添加迁移时遇到了这个烦人的警告:

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'.

这个警告的定义在这里:https://github.com/dotnet/efcore/blob/main/src/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs

我不确定理解这个警告的目的,因为带有[Keyless]标记的实体不存储在数据库中。另外,添加类似[Column(TypeName = "decimal(28, 6)")]的属性似乎不能消除关于keyless实体的警告。

2个回答

23

显然,EF Core 6有一个新的属性

using Microsoft.EntityFrameworkCore;

...

[Precision(18, 2)]

1
太好了,谢谢。实际上,HasPrecision的要求对应于标签[Precision(18, 2)]而不是[Decimal(18, 2)]。 - sofsntp
谢谢这个提示。帮助了我很多! - Vikingslord

0
在您的 IEntityTypeConfiguration 实现中,您还可以使用 .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); 
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接