I have a class containing a DateTime property. How to correctly save time using Entity Framework Core? I will give an example below.
I use the TIMESTAMP format to store the DateTime in the database. I have a server and database (MySQL) that have timezone +3 (Moscow).
SELECT TIMEDIFF(current_timestamp(), utc_timestamp()); // result = 03:00:00
I also have a program that can run on the PC of various users. The program saves the object containing the DateTime property in the database:
obj.DateTime = DateTime.Parse("2020-10-13T19:00:00+0300");
Now let's look at a few situations:
The program is launched on a PC with a time zone of +3 (Moscow)
Console.WriteLine(obj.DateTime?.ToString()); // 2020-10-13 19:00:00
I check the data in the database using phpMyAdmin and I see 2020-10-13 19:00:00. Everything is OK. The time zone of the PC is equal to the time zone of the database, so we see the same result.
The program is open on a PC with a time zone of +6:30 (Yangon)
Console.WriteLine(obj.DateTime?.ToString()); // 13.10.2020 22:30:00
I check the data in the database using phpMyAdmin and I see 2020-10-13 22:30:00. Fail! I should have seen everything at the same time 19:00, not 22:30. The obj.DateTime property contained the same time!
How can I now understand that these records contain equal time? Why does the EF not transfer DateTime to the UTC?