Gavin Draper's Blog

Father, Developer, Tech Geek, Extreme Sports Fanatic

@gavdraper | +gavdraper | github | StackOverflow Careers
 

Entity Framework Code First Rounding Decimal Model Properties

If you use Entity Framework code first and user decimal properties with a scale greater than 2 then you will find that by default the table gets generated with a scale of 2 and values get rounded to 2 decimal places.

In order to tell the Entity Framework the scale and precision of these properties you need to override the OnModelCreating event in your data context. For example...

  
protected override void OnModelCreating(DbModelBuilder modelBuilder)  
{
    modelBuilder.Entity<Activity>().Property(a => a.Latitude).HasPrecision(18, 9);
    modelBuilder.Entity<Activity>().Property(a => a.Longitude).HasPrecision(18, 9);
}

You will also either need to get the framework to regenerate your database, or manually edit your existing database so the decimal fields have the right precision and scale e.g Decimal(18,9)


 
comments powered by Disqus