Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping of TimeSpan to DbType.Time is too restrictive #319

Open
StevenGilligan opened this issue Jul 17, 2015 · 1 comment
Open

Mapping of TimeSpan to DbType.Time is too restrictive #319

StevenGilligan opened this issue Jul 17, 2015 · 1 comment
Milestone

Comments

@StevenGilligan
Copy link

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]
public void TestDapperTimespan()
{
    var connection = new SqlConnection(myConnectionString);
    var ts = 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));
}
@kwaclaw
Copy link

kwaclaw commented Sep 22, 2016

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants