Skip to content

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

@StevenGilligan

Description

@StevenGilligan

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));
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions