0

I am using Entity Framework 5 code first to talk to an Oracle 11g or 12c database (I've verified the problem in both versions). The Oracle field is a FLOAT type while the field in my POCO is a decimal. I'm using decimal because I need my decimal places to be accurate up to 5 digits.

When I save my entity, the resulting record in the database always rounds the value to 2 decimal places.

I have verified through a database tool (Toad) that the column will support precision of 5. I cannot change the data type in the database due to backwards compatibility. I have found that using a double does not have the same problem. But double is notorious for giving inexact numbers, especially when multiple mathematical operations are performed.

Does anyone know why a decimal value would be truncated? I am using the Oracle data provider.

esteuart
  • 1,323
  • 1
  • 11
  • 14

1 Answers1

1

The link provided by @Grant in the comments above provided the answer. I will paraphrase here. The default mapping for a decimal value is to an Oracle DECIMAL(18,2). This is why it was rounding to two decimal places. In order to change the default behavior, you have to add a statement in the OnModelCreating override inside the Context class. (In EF6 you can change the convention for all of the decimal fields at once as noted here.)

Change Decimal Mapping for a Particular Decimal Field

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    //...

    modelBuilder.Entity<MyEntity>().Property(x => x.MyProperty).HasPrecision(18, 5);

    //...
}
Community
  • 1
  • 1
esteuart
  • 1,323
  • 1
  • 11
  • 14