Dapper maps TimeSpan to DbType.Time and I believe this is the wrong mapping for TimeSpan.
A TimeSpan is a struct that is represented by a single int64 value (ticks). The best way to represent that in a Sql Database is by having a BigInt field. By using DbType.Time instead, we're restricted to a very narrow range of 00:00:00.0000000 through 23:59:59.9999999 (Time sql type on msdn). By using a BigInt, it would be possible to map all values of a TimeSpan which means +/- approx. 29,227 years.
Using NHibernate for example, the mapping between TimeSpan and BigInt works out of the box.
Example test case that throws an exception :
[Test]publicvoidTestDapperTimespan(){varconnection=newSqlConnection(myConnectionString);varts=connection.Query<TimeSpan>("select @ts",new{ts=TimeSpan.FromDays(3)});// System.OverflowException : SqlDbType.Time overflow. Value '3.00:00:00' is out of range. Must be between 00:00:00.0000000 and 23:59:59.9999999.Assert.AreEqual(ts,TimeSpan.FromDays(3));}
Dapper maps TimeSpan to DbType.Time and I believe this is the wrong mapping for TimeSpan.
A TimeSpan is a struct that is represented by a single int64 value (ticks). The best way to represent that in a Sql Database is by having a BigInt field. By using DbType.Time instead, we're restricted to a very narrow range of 00:00:00.0000000 through 23:59:59.9999999 (Time sql type on msdn). By using a BigInt, it would be possible to map all values of a TimeSpan which means +/- approx. 29,227 years.
Using NHibernate for example, the mapping between TimeSpan and BigInt works out of the box.
Example test case that throws an exception :