Search code examples
entity-frameworkdecimalbreezescale

Breeze - Saving decimal type has wrong scale?


i'm using the latest Breezejs (1.5.3) for data operations in sql server with Entity Framework. The save operation is done successfully. the savemap has the decimal value with full scale but after update the field in DB has wrong scale (number of decimal points).
e.g the savemap has the value 565.13435435 to be persisted in db field decimal(19,5) but after the update the field has 2 decimal points


Solution

  • This is an EF problem. The default scale for decimal types in EF is 2. You have to tell EF what the scale is for your column. If you only need 4 decimal places, you could use a shortcut by saying it is a Money type:

    [Column(TypeName = "Money")]
    public decimal MyDecimalValue { get; set; }
    

    But for all other cases, you need to specify the precision in the OnModelCreating method of your DbContext:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // configure the precision to match what is specified in the database
        modelBuilder.Entity<MyEntity>().Property(p => p.MyDecimalValue).HasPrecision(19, 5);
    }