Monday 21 July 2014

MVC 4, EF5 Code First - Forcing a Decimal to x Number of Places

Stick this in your DB Context if you need to have EF recongise more than two decimal places.

  protected override void OnModelCreating(DbModelBuilder modelBuilder)  
     {  
       // We have a Decimal Field in Our SQL Table decimal(18, 4)  
       // By default only the first two digits after the decimal will be recognised if this property is set to type decimal  
       // we need to override this behaviour and here's how  
       modelBuilder.Entity<MyObjec t>().Property(f => f.totalWeightMg).HasPrecision(18,4);       
       base.OnModelCreating(modelBuilder);  
     }  

No comments: