29

Possible Duplicate:
Entity Framework Code First: decimal precision

I'm using Linq-to-Entities with the EntityFramework 4.1 Code First system. All of my decimal properties are being truncated to two decimal places on save. I can examine the object being modified and can see in the debugger that is has the right number of decimal places and I can hard code the value in the database to show that it can accept the right number of decimal places [in this case, decimal(4,3)]. But when I save the value, it never saves it correctly, but instead truncates decimal values to two decimal places. I haven't been able to find a class in System.ComponentModel.DataAnnotations that allows you to specify precision. The class (sanitized), for reference is:

public class Metrics
{
    public decimal? PPM { get; set; }
}
Community
  • 1
  • 1
Orion Adrian
  • 19,053
  • 13
  • 51
  • 67

1 Answers1

41
public class MyContext : DbContext
{
    public DbSet<Metrics> Metrics { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Metrics>().Property(x => x.PPM).HasPrecision(4, 3);
    }
}
Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154