TimeSpan timers get +1 after saving

I have a problem with dapper, I don't know how to fix it:

I have Poco like this:

public class Test { public long Id { get; set; } public TimeSpan? Time { get; set; } } 

The Time field is the MySQL TIME field. If I download a line with Dapper with a time field with 1000 ticks, for example, and I keep this Poco unchanged, reload the same line again, the "Time" field is now at 1001 Ticks.

What am I doing wrong?

EDIT:

How do I load my line:

 var testobj = Db.Query<Test>("select * from Test where Id = @id", new {id = Id}); 

How do I save it:

 Db.Execute("replace into Test values (@Id,@Time)", testObj); 

EDIT 2:

Time to save object:

 {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 saving:

 {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 hint with my Test class as follows:

 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

+5
source share
3 answers

After much research, the mysql plugin was developed by my company, which in some cases does something special. Sorry for wasting time on this issue.

+1
source

My play attempt is working 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 real problem: I will need help to reproduce it. Everything seems to be working fine.

0
source

I assume that the Time field changes several times (overwrites). Maybe a different thread.

To make sure, make the readonly field and initialize it once in the constructor, and all subsequent attempts to change this field will be deprived by the compiler.

0
source

Source: https://habr.com/ru/post/1239070/


All Articles