zoukankan      html  css  js  c++  java
  • entityFramework 中decimal精度缺失问题

    在entityFramework中,decimal精度默认为2位数,当要设置的精度大于2位并且数据库中设置的decimal精度大于2位时,则将数据保存在数据库中后两位的小数内容将强制为00

    解决方案:在DbContext中加下如下代码

    modelBuilder.Entity<t_freshgoods>().Property(x => x.sale_price).HasPrecision(10, 4);

     public WXMallDbContext()
                : base("WXMallDbContext")
            {
            }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                //移除复数表名
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
                //移除级联删除关系
                //modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
                //modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
                //防止黑幕交易 要不然每次都要访问 EdmMetadata这个表
    
                base.OnModelCreating(modelBuilder);
    
                //此处的代码用于修复小数点精度问题  防止4位小数点的数据后两位强制为00
                modelBuilder.Entity<t_freshgoods>().Property(x => x.sale_price).HasPrecision(10, 4);
                modelBuilder.Entity<t_freshgoods>().Property(x => x.sale_qty).HasPrecision(10, 4);
                modelBuilder.Entity<t_freshgoods>().Property(x => x.sale_amt).HasPrecision(10, 4);
                modelBuilder.Entity<t_freshgoods>().Property(x => x.source_price).HasPrecision(10, 4);
            }
    

      

  • 相关阅读:
    CF733F
    P4826
    洛谷P2687 & P1108
    CF42A
    洛谷P1858
    CF1428C
    洛谷P4981
    树形DP
    背包六讲(也不知道为啥就是六个 $QwQ$)
    2020
  • 原文地址:https://www.cnblogs.com/WQ1992/p/9583015.html
Copyright © 2011-2022 走看看