You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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));}
The text was updated successfully, but these errors were encountered:
I tried a custom TypeHandler as a workaround.
However, since the SetValue method for such a TypeHandler is never called (there are several open issues on that) it is not applicable to my use case.
I would like to know what other workarounds anyone has come up with (other than special treatment in the application code).
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 :
The text was updated successfully, but these errors were encountered: