DapperLib / DapperLib/Dapper

Mapping of TimeSpan to DbType.Time is too restrictive

Open
#319 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

breaking-change
Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the shown SqlConnection, Query, and a parameterized TimeSpan.FromDays(3) value, then trace where Dapper assigns DbType.Time to TimeSpan parameters. Done means the example no longer overflows and a regression test confirms the full TimeSpan value can be queried successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.