2021-0915

EF decimal默认两位经度修改

作者: momy 分类: 黑客动态 0 Comment »
摘要:EF中默认的decimal数据精度为两位数,当我们数据库设置的精度大于2时,EF将只会保留到2为精度。

EF中默认的decimal数据精度为两位数,当我们数据库设置的精度大于2时,EF将只会保留到2为精度。


可使用如下方法将其修改为4位精度:


EF

protectedoverridevoidOnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Properties<decimal>().Configure(config => config.HasPrecision(18, 4));
}


EF CORE:

foreach (var property in modelBuilder.Model.GetEntityTypes().SelectMany(t => t.GetProperties()).Where(p => p.ClrType == typeof(decimal) || p.ClrType == typeof(decimal?)))
{
    property.SetColumnType("decimal(18,4)");
}


标签: EF decimal 精度 阅读: 12438
上一篇: DataGrip永久试用方法 - 16931次
下一篇: Nhibernate访问Oracle数据库命不中索引,执行慢问题 - 9797次

向右滑动解锁留言