Why EF5 Not Saving Decimal Precision Correctly

Entity Framework Not Saving Decimal Precision Correctly : DecimalPropertyConfiguration.HasPrecision  and there is now a DecimalPropertyConfiguration.HasPrecision Method to play with precision of specific property of a entity class. Let see some code:  public DecimalPropertyConfiguration HasPrecision( byte precision, byte scale ) where precision is the total number of digits the db will store, regardless of where the decimal point falls and scale is the number of decimal places it will store.  Therefore there is no need to iterate through properties and this can be called for any property as below:  public class EFDbContext : DbContext {    protected override void  OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)    {        modelBuilder.Entity<Class>().Property(object => object.property).HasPrecision(12, 10);          base.OnModelCreating(modelBuilder);    } } So basically you just have to add specific precision and scale using HasPrecision for the appropriate property of your class that tell your model to configure it this way.
Entity Framework Not Saving Decimal Precision Correctly

Let say you have a table in database with datatype decimal(18,3) which is most common datatype to store decimal values in SQL Server. You can easily change the precision(total char length of attribute ) and scale(number of char after decimal point) by going into design.

If yo are using Entity Framework ( >4.1 ) and then you try to save values like 1.111 to the field but it saves 1.110; Bummer!!!

Here the thing starts looking like weird. There are 2 important changes, from EF 4.1 onwards:

DbModelBuilder

the ModelBuilder class is now DbModelBuilder 

DecimalPropertyConfiguration.HasPrecision

and there is now a DecimalPropertyConfiguration.HasPrecision Method to play with precision of specific property of a entity class. Let see some code:


public DecimalPropertyConfiguration HasPrecision( byte precision, byte scale )

where precision is the total number of digits the db will store, regardless of where the decimal point falls and scale is the number of decimal places it will store.

Therefore there is no need to iterate through properties and this can be called for any property as below:


public class EFDbContext : DbContext
{
   protected override void  OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
   {
       modelBuilder.Entity<Class>().Property(object => object.property).HasPrecision(12, 10);

       base.OnModelCreating(modelBuilder);
   }
}

So basically you just have to add specific precision and scale using HasPrecision for the appropriate property of your class that tell your model to configure it this way.

Cheers!!

Advertisements

Say something : I accept all the "Humer&Critic"

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s