EntityFramwork decimal保存到数据库精度只有小数点后两位

  • EntityFramwork decimal保存到数据库精度只有小数点后两位已关闭评论
  • 169 次浏览
  • A+
所属分类:.NET技术
摘要

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

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

如 product.Price=0.0111,存到数据库会变成0.0100

1.解决方案一是网上找到常见的方法为重写DbContext的OnModelCreating方法:

protected override void OnModelCreating(DbModelBuilder modelBuilder) {               modelBuilder.Entity<Product>().Property(x => x.Price).HasPrecision(18, 5); }

但如果数据表多或者Decimal类型字段多的话,用OnModelCreating的方法将会变得相当冗余,而且不便管理。

2.解决方案二是我推荐使用Attribute属性标签进行设置,在Entity Model class中decimal的字段上方直接添加自定义拓展的属性标签即可。

调用:

EntityFramwork decimal保存到数据库精度只有小数点后两位

 

其中 [DecimalPrecision(18, 5)]即是我们自定义的精度Attribute

具体实现代码如下:

 /// <summary>      /// <para>自定义Decimal类型的精度属性</para>      /// </summary>      [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]      public   class DecimalPrecisionAttribute : Attribute      {            #region Field          private byte _precision = 18;          public byte _scale = 5;          #endregion            #region Construct          /// <summary>          /// <para>自定义Decimal类型的精确度属性</para>          /// </summary>          /// <param name="precision">precision          /// <para>精度(默认18)</para></param>          /// <param name="scale">scale          /// <para>小数位数(默认5)</para></param>          public DecimalPrecisionAttribute(byte precision = 18, byte scale = 5)          {              Precision = precision;              Scale = scale;          }           #endregion                    #region Property          /// <summary>          /// 精确度(默认18)          /// </summary>          public byte Precision          {              get { return this._precision; }              set { this._precision = value; }          }            /// <summary>          /// 保留位数(默认5)          /// </summary>          public byte Scale          {              get { return this._scale; }              set { this._scale = value; }          }           #endregion      }

  /// <summary>     /// 用于modelBuilder全局设置自定义精度属性     /// </summary>     public class DecimalPrecisionAttributeConvention         : PrimitivePropertyAttributeConfigurationConvention<DecimalPrecisionAttribute>     {         public override void Apply(ConventionPrimitivePropertyConfiguration configuration, DecimalPrecisionAttribute attribute)         {             if (attribute.Precision< 1 || attribute.Precision> 38)             {                 throw new InvalidOperationException("Precision must be between 1 and 38.");             }             if (attribute.Scale > attribute.Precision)             {                 throw new InvalidOperationException("Scale must be between 0 and the Precision value.");             }             configuration.HasPrecision(attribute.Precision, attribute.Scale);         }     }

 

 

再在DbContext重写OnModelCreating,添加自定义的DecimalPrecisionAttributeConvention即可以方便地任意添加需要精度控制的字段。

    public class Project_DbContext : DbContext     {         public Project_DbContext() : base("DefaultConnection") { }          protected override void OnModelCreating(DbModelBuilder modelBuilder)         {             modelBuilder.Conventions.Add(new DecimalPrecisionAttributeConvention());             base.OnModelCreating(modelBuilder);         }      }

 

转载于:https://www.cnblogs.com/lonelyxmas/p/9962627.html