Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent decimal values from being truncated to 2 places on save using the EntityFramework 4.1 CodeFirst? [duplicate]

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; } } 
like image 696
Orion Adrian Avatar asked Jul 08 '11 19:07

Orion Adrian


1 Answers

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);     } } 
like image 142
Diego Mijelshon Avatar answered Oct 04 '22 22:10

Diego Mijelshon