Parameter value '<decimal>' is out of range

3 posts, 1 answers
  1. Gary
    Gary avatar
    28 posts
    Member since:
    Jun 2015

    Posted 27 Oct 2015 Link to this post

    I am completely confused and have lost the better part of my day to this issue. 

    I have some fields in my database table defined as ... 

     

    [SheetWidth]            DECIMAL(6,2) NULL,
    [SheetHeight]           DECIMAL(6,2) NULL,
    [SheetThickness]        DECIMAL(6,2) NULL,

    They are associated with the following properties on a model ... 

     

    public Decimal? SheetWidth { get; set; }
     
    public Decimal? SheetHeight { get; set; }
     
    public Decimal? SheetThickness { get; set; }

     They are mapped in the metadatasource ... 

     

    map.HasProperty(e => e.SheetWidth)
        .HasColumnType("decimal")
        .HasScale(6)
        .HasPrecision(2)
        .IsNullable();
     
    map.HasProperty(e => e.SheetHeight)
        .HasColumnType("decimal")
        .HasScale(6)
        .HasPrecision(2)
        .IsNullable();
     
    map.HasProperty(e => e.SheetThickness)
        .HasColumnType("decimal")
        .HasScale(6)
        .HasPrecision(2)
        .IsNullable();

     

     I am attempting to create a new record from a call to a WebAPI service by passing in the following JSON (which maps to the model) ... 

     

    {
        "SheetWidth": 6.5,
        "SheetHeight": 8.5,
        "SheetThickness": null,
    }

     Everything seems to go fine until the SaveChanges() method is called and I get the following response back ... 

     

     "Insert of '594112938-24' failed: System.ArgumentException: Parameter value '8.​500000' is out of range"

     

    I understand why I am getting the error ... clearly 8.500000 ... is out of the (6, 2) scale/precision range that I had specified ... the question I have is WHY is 8.5 being converted into 8.500000. While in debug mode I observed the value of the all the way to the "SaveChanges()" method and it remained 8.5. Based on the data types I assigned I would have expected it to be saved as ... 8.50 ... which is exactly the behavior I want. 

     

    So what's going on inside "SaveChanges()" that killing my datatype? And what can I do about it?

     

     

     

     

     

     

     

     

     

     

     

     

     

  2. Answer
    Ady
    Admin
    Ady avatar
    589 posts

    Posted 30 Oct 2015 Link to this post

    Hi Gary,

     It seems you have got the Fluent mapping wrong. It should be 'HasPrecision(6).HasScale(2)' and not the other way round.
    Can you fix this and check if you still get the error?

    Regards,
    Ady
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  3. DevCraft banner
  4. Gary
    Gary avatar
    28 posts
    Member since:
    Jun 2015

    Posted 02 Nov 2015 in reply to Ady Link to this post

    FOR SHAME ON ME!! You were correct! I completely had them backwards. DOH!
Back to Top