tm929 / dapper-dot-net

Automatically exported from code.google.com/p/dapper-dot-net
Other
0 stars 0 forks source link

TimeSpan and Db.Time not supported? #70

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
I'm trying to map a TimeSpan field to a Db.Time column using cnn.Query<T>(sql, 
row) function.
It seems this mapping is not configured in SqlMapper and I get the following 
exception.
Now I have to create a non-template DynamicParameter and convert TimeSpan type 
to DateTime like this:
    var p = new Dapper.DynamicParameters();
    p.Add("@Code", row.Code);
    p.Add("@Name", row.Name);
    p.Add("@AfternoonCloseTime", DateTime.Today.Add(row.AfternoonCloseTime));
    cnn.Query<decimal>(sql, p);

What is the expected output? What do you see instead?
System.NotSupportedException was unhandled
  Message=The member AfternoonCloseTime of type System.TimeSpan cannot be used as a parameter value
  StackTrace:
       at Dapper.SqlMapper.LookupDbType(Type type, String name) in ...\Dapper\SqlMapper.cs:line 324
       at Dapper.SqlMapper.CreateParamInfoGenerator(Identity identity) in ...\Dapper\SqlMapper.cs:line 1239
       at Dapper.SqlMapper.GetCacheInfo(Identity identity) in ...\Dapper\SqlMapper.cs:line 914
       at Dapper.SqlMapper.<QueryInternal>d__13`1.MoveNext() in ...\Dapper\SqlMapper.cs:line 577

...

What version of the product are you using? On what operating system?
1.7
Win7 x64
SQL Server 2008

Please provide any additional information below.

Original issue reported on code.google.com by thu...@gmail.com on 9 Nov 2011 at 4:48

GoogleCodeExporter commented 8 years ago
Suggested solution:

Add the following code to the constructor of SqlMapper:

...
typeMap[typeof(DateTime)] = DbType.DateTime;
typeMap[typeof(DateTimeOffset)] = DbType.DateTimeOffset;
typeMap[typeof(TimeSpan)] = DbType.Time;                 <--- This one ...
...

...
typeMap[typeof(DateTime?)] = DbType.DateTime;
typeMap[typeof(DateTimeOffset?)] = DbType.DateTimeOffset;
typeMap[typeof(TimeSpan?)] = DbType.Time;                <--- ... and this one.
...

That seems to have worked for me.

Original comment by emil.b...@gmail.com on 6 Mar 2012 at 10:50

GoogleCodeExporter commented 8 years ago
Any chance of getting this rolled into the build distributed from NuGet?  I can 
work around it, but it's a bit ugly all things considered.

Original comment by casper...@caspershouse.com on 24 Jun 2012 at 7:39

GoogleCodeExporter commented 8 years ago
Noted. I'll work through a few outstanding, probably tomorrow.

Original comment by marc.gravell on 24 Jun 2012 at 7:52

GoogleCodeExporter commented 8 years ago
Any chance of getting this into a NuGet build?  I'm running into the same 
problem when trying to work with time data types.

Original comment by asp...@gmail.com on 16 May 2013 at 1:45

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
This has been fixed for a long time

Original comment by marc.gravell on 5 Aug 2014 at 3:47