DapperLib/Dapper.Contrib

Insert<T> problem with identity exceeding int max value

Opened this issue · 1 comments

Hi,

we started experiencing problems after our table identity reached the max int value (2,147,483,647).
After deep checking the SqlMapperExtensions code, I noticed that even if the method signature is to return a "long" type, the return variable is defined as "int"; what happens is that the record is correctly inserted but the library throws a System.OverflowException when returning the identity value due to the implicit cast from int to long.

SqlMapperExtensions code :
int returnVal;
var wasClosed = connection.State == ConnectionState.Closed;
[...]
return returnVal;

For the moment the temporary fix is to pass to the method a List containing just one entity, so that the return value is the number of inserted rows, but this is working because we don't need the identity back after this insert operation.
By the way our Entity class has the id field of type "long" and tagged as "key", and the problem disappear if we force the identity on DB to a value lower than the max int, and of course it's reproduced by forcing back to a higher value.

Thanks for your attention, cheers!

This bug just bit us. It's a bit of a ticking timebomb for anyone using bigint PKs in their DB. Thankfully we noticed it in our testing environment first (which has more data). But good lord can we please get this fixed?! Otherwise Dapper.Contrib is going to need to be completely refactored out of our entire codebase (A LOT OF WORK)!

@iltizio thank you so much for that clever work around! (Which is also pretty unfortunate that the return value has two different meanings depending on the input).