EF (ef core,core first)设置decimal字段精度

  • Post author:
  • Post category:其他


1、网上的方法

[Column(TypeName = "decimal(18,2)")]
public decimal Money { get; set; }

以上经测试不管用 不知道是不是我配置的问题

2、DbContext重写OnModelCreating 经测试还是不管用

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Product>().Property(t => t.UnitPrice)
        .HasColumnName("UnitPrice")
        .HasPrecision(18, 2);
}

在以上方法加上base.OnModelCreating(modelBuilder);经测试好用

protected override void OnModelCreating(DbModelBuilder modelBuilder)

{

modelBuilder.Entity<Product>().Property(t => t.UnitPrice) .HasColumnName(“UnitPrice”) .HasPrecision(18, 2);

base.OnModelCreating(modelBuilder);

}

3、DbContext重写OnModelCreating 另一种方式 经测试还是不管用

 protected override void OnModelCreating(ModelBuilder modelBuilder)
 {
       base.OnModelCreating(modelBuilder);
       modelBuilder.Entity<PayPay>().Property(p => p.Money).HasColumnType("decimal(18,2)");
 }

4、网上推荐使用

Attribute

属性标签进行设置,在Entity Model class中decimal的字段上方直接添加自定义拓展的属性标签即可。经测试不管用 可能我配置的问题 如有测试成功的请多多指导

e.g.

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

具体实现代码如下:

复制代码

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

    }



版权声明:本文为huiguoyuan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。