C# 的decimal精度入庫的問題

首先說一下C# 和 SQL 中的decimal默認都是 (18,2) 。

就算SQL把精度改成 (18,4),C#進去的數據依舊是(18,2),第三四位小數,根本進不了,比如插入0.1234直接數據庫成0.1200

那麼可能會出現統計的一些問題-----對不齊。。。然後領導各種噴。。

 

mvc中,需要對字段重新定義字段類型,就是codefirst的那一套。

摘要:
允許爲模型中的實體類型執行配置。可以通過對 System.Data.Entity.DbModelBuilder 使用 Entity 方法來獲取 EntityTypeConfiguration,也可以通過對System.Data.Entity.DbModelBuilder 使用 Configurations 屬性來註冊從 EntityTypeConfiguration派生的自定義類型。

 

/// <summary>
/// 類
/// </summary>
[Table("St_Detailed")]
public class DetailedModel
{
     [Key]
     [Display(Name = "編號"), Required]
     public Guid ID { get; set; }

     ......
}
public class DetailedConfiguration : EntityTypeConfiguration<DetailedModel>
{
     public DetailedConfiguration()
     {
         Property(t => t.Ratio).HasPrecision(9, 2);
         Property(t => t.ContractNo).IsUnicode(false);
         Property(t => t.Achievement).HasPrecision(18, 4);
         Property(t => t.SignTime).HasColumnType("date");
     }
}



public class BaseContext : DbContext
{
    public BaseContext() : base("name=Context")
    { }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new DetailedConfiguration());
    }
}



這樣就能保證4位小數入庫了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章