DapperLib / DapperLib/Dapper

ISNULL on nullable decimal property when value is null uses wrong decimal precision

Open
#1,130 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

We recently found an issue with one of our Dapper queries where it was rounding a decimal value up. Below is a brief example and solution.

Table Definition

CREATE TABLE [dbo].[Foo] (
    [Value] [decimal](19, 5) NULL
)

Model

public class Foo {
    decimal? Value { get; set; }
}

Problem Query

var foo = new Foo { Value = null };

conn.Execute(@"
    update dbo.Foo
    set Value = ISNULL(@Value, Value)
", foo);

Working Query

var foo = new Foo { Value = null };

conn.Execute(@"
    update dbo.Foo
    set Value = ISNULL(CAST(@Value as decimal(19,5)), Value)
", foo);

The problem query generates the following SQL:

exec sp_executesql N'
    update dbo.Foo
    set Value = ISNULL(@Value, Value)',@Value decimal(29,0)										

As you can see, the inferred datatype is using the wrong precision of (29,0) which causes the existing Value to be rounded when in the ISNULL.

Not sure how/if this can be fixed. But thought I'd report it. Hope it's clear. Let me know if more detail will help.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository files or tests are named. Start by reproducing the nullable decimal and ISNULL example from the issue, then trace Dapper's parameter type inference to determine why a null value becomes decimal(29,0). Done means the existing decimal(19,5) value is not rounded when the nullable parameter is null, without requiring the SQL CAST workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.