LoginSignup
1
2

More than 5 years have passed since last update.

EntityFramework 6でDecimalの値が小数点第3位で四捨五入されてしまう場合の対処法

Last updated at Posted at 2016-02-19

ASP.NET MVC + Entity Framework 6 + OracleのDB FirstでWebアプリを作っていて、画面から入力した数値が、更新時に小数点第3位で四捨五入されてしまう問題に遭遇したので、対処法を記録しておきます。

「entity framework decimal mapping」の検索ワードで出てきた Entity Framework Data Type Mapping を見てみると、Decimal型のプロパティはDecimalPropertyConventionクラスによってNumber(18,2)にマッピングされているようです。

DbContextを継承したクラスでDecimalPropertyConventionを無効化することによって解決しました。

Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
    ' disable decimal property conversion
    modelBuilder.Conventions.Remove(Of DecimalPropertyConvention)()
End Sub

プロパティ個別に設定したい場合は、こちらが参考になりそうです。
stackoverflow - Decimal precision and scale in EF Code First

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2