I have an issue with dapper, I don't know how to fix this :
I have a Poco like this :
public class Test
{
public long Id { get; set; }
public TimeSpan? Time { get; set; }
}
The field Time is a MySQL 'TIME'. If I load a row with Dapper with a Time field with 1000 ticks for example, and I save this Poco without change anything, reload the same row again, Time field is now at 1001 Ticks.
What am I doing wrong ?
EDIT :
How I load my row :
var testobj = Db.Query<Test>("select * from Test where Id = @id", new {id = Id});
How I save it :
Db.Execute("replace into Test values (@Id,@Time)", testObj);
EDIT 2 :
A timespan object before save :
{15:22:24}
Days: 0
Hours: 15
Milliseconds: 0
Minutes: 22
Seconds: 24
Ticks: 553440000000
TotalDays: 0.64055555555555554
TotalHours: 15.373333333333333
TotalMilliseconds: 55344000.0
TotalMinutes: 922.4
TotalSeconds: 55344.0
and after save :
{15:22:25}
Days: 0
Hours: 15
Milliseconds: 0
Minutes: 22
Seconds: 25
Ticks: 553450000000
TotalDays: 0.64056712962962958
TotalHours: 15.37361111111111
TotalMilliseconds: 55345000.0
TotalMinutes: 922.41666666666674
TotalSeconds: 55345.0
You can see that Ticks 553440000000 and become 553450000000
EDIT 3 :
I use Hans tip with my Test class like this :
public class Test
{
public long Id { get; set; }
private TimeSpan? _time;
public TimeSpan? Time
{
get
{
if (_time.HasValue)
return TimeSpan.FromTicks((long)Math.Floor(_time.Value.Ticks / 100000000d) * 100000000);
return _time;
}
set { _time = value; }
}
}
and it works, but it's still odd
After much research, there was a mysql plugin developed by my company that doing something special in some cases. Sorry for the loss of time on this issue .
My attempt to repro works fine:
[FactMySql]
public void Issue426_SO34439033_DateTimeGainsTicks()
{
using (var conn = GetMySqlConnection())
{
try { conn.Execute("drop table Issue426_Test"); } catch { }
try { conn.Execute("create table Issue426_Test (Id int not null, Time time not null)"); } catch { }
const long ticks = 553440000000;
const int Id = 426;
var localObj = new Issue426_Test
{
Id = Id,
Time = TimeSpan.FromTicks(ticks) // from code example
};
conn.Execute("replace into Issue426_Test values (@Id,@Time)", localObj);
var dbObj = conn.Query<Issue426_Test>("select * from Issue426_Test where Id = @id", new { id = Id }).Single();
dbObj.Id.IsEqualTo(Id);
dbObj.Time.Value.Ticks.IsEqualTo(ticks);
}
}
Whatever the actual problem is: I'm going to need some help to reproduce it. It seems to work just fine.