Dapper,decimal to double? Error parsing column X

dapper oracle parsing

Question

My database is Oracle. I user Dapper 1.13, which throws an exception saying Error parsing column 3 (LATITUDE=39.2330 - Decimal). LATITUDE is a double? type in my entity.

Please help me.

Thanks

Accepted Answer

Yep, that's a bug; have fixed locally - basically, change line 2367 (wow, when did that file get so big?) from:

il.Emit(OpCodes.Ldtoken, unboxType);

to:

il.Emit(OpCodes.Ldtoken, Nullable.GetUnderlyingType(unboxType) ?? unboxType);

Basically, it is emitting: (decimal?)Convert.ChangeType(val, typeof(decimal?)) when it should be emitting (decimal?)Convert.ChangeType(val, typeof(decimal)) (note the all-important ? in the typeof).

Edit: actually, the fix that I have pushed to git is a bit more complex than just fixing the Convert.ChangeType - it now resolves operators as-needed, so the decimal <===> double conversions uses IL static-call.

This will be fixed in the next build. Or you can build locally. The following now passes:

public void TestDoubleDecimalConversions_SO18228523_RightWay()
{
    var row = connection.Query<HasDoubleDecimal>(
        "select cast(1 as float) as A, cast(2 as float) as B, cast(3 as decimal) as C, cast(4 as decimal) as D").Single();
    row.A.Equals(1.0);
    row.B.Equals(2.0);
    row.C.Equals(3.0M);
    row.D.Equals(4.0M);
}
public void TestDoubleDecimalConversions_SO18228523_WrongWay()
{
    var row = connection.Query<HasDoubleDecimal>(
        "select cast(1 as decimal) as A, cast(2 as decimal) as B, cast(3 as float) as C, cast(4 as float) as D").Single();
    row.A.Equals(1.0);
    row.B.Equals(2.0);
    row.C.Equals(3.0M);
    row.D.Equals(4.0M);
}
public void TestDoubleDecimalConversions_SO18228523_Nulls()
{
    var row = connection.Query<HasDoubleDecimal>(
        "select cast(null as decimal) as A, cast(null as decimal) as B, cast(null as float) as C, cast(null as float) as D").Single();
    row.A.Equals(0.0);
    row.B.IsNull();
    row.C.Equals(0.0M);
    row.D.IsNull();
}

class HasDoubleDecimal
{
    public double A { get; set; }
    public double? B { get; set; }
    public decimal C { get; set; }
    public decimal? D { get; set; }
}



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why