Masataka Miki's Blog

すみません、わかりません。もっと勉強して改善します。

[C#/MVC]EntityFrameworkでdecimal型のメンバが小数点以下3桁目で丸められるという現象

   

EntityFramework 6.2.0で、decimal型のメンバがDB(SQL Server)登録後に確認すると、小数点以下3桁目で丸められていた。

Entity Framework Data Type Mapping を確認すると、Number(18,2)でマッピングされていた。

Mapping of this type depends on the DecimalPropertyConvention. If this convention is enabled (it is enabled by default), System.Decimal is mapped to NUMBER(18,2). Otherwise, it is mapped to NUMBER.

DbContextを継承したクラスに、以下の処置を行い、対応した。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
modelBuilder.Conventions.Add(new DecimalPropertyConvention(38, 18));
}

 - 技術 , ,