0

I have a model with uint? column, but I receive a "Arithmetic operation resulted in an overflow" exception for simple query:

connection.QuerySingleOrDefault<Test>("Select * from Test")

In the database this column is created with bigint type.

I can't find information about what the dapper does not support unit types and mapping should work. Any suggestions?

enter image description here

Sheinar
  • 362
  • 3
  • 14

2 Answers2

0

A uint can store: 0 to 4,294,967,295

A long can store: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Your query returns a value larger then 4,294,967,295 or a negative resulting in the arithmetic overflow.

You should map your database BIGINT columns to c# long.

Source: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types

Preben Huybrechts
  • 5,853
  • 2
  • 27
  • 63
  • The value in db is 3822735785. I've added a stack trace now, sorry I didn't do it earlier. – Sheinar Dec 28 '20 at 08:13
  • The value realy doesn't matter. The database can store bigint, so your code should expect to work with bigint/long. If you still want to ignore this you can add a custom type mapping: https://stackoverflow.com/questions/8902674/manually-map-column-names-with-class-properties/12615036#12615036 – Preben Huybrechts Dec 28 '20 at 08:16
  • Thanks, the custom value handler helped – Sheinar Dec 28 '20 at 08:59
0

It works when I added a custom value handler

public class UintHandler : SqlMapper.TypeHandler<uint?>
   {
      /// <inheritdoc />
      public override uint? Parse(object value)
      {
         if (value == null)
         {
            return null;
         }

         return Convert.ToUInt32(value, CultureInfo.InvariantCulture);
      }

      /// <inheritdoc />
      public override void SetValue(IDbDataParameter parameter, uint? value)
      {
         if (parameter == null)
         {
            return;
         }

         parameter.DbType = DbType.UInt32;
         parameter.Value = value;
      }
   }

And register:

SqlMapper.AddTypeHandler(new UintHandler());
Sheinar
  • 362
  • 3
  • 14