Closed anttikes closed 3 years ago
SDK uses Newtonsoft.Json by default, Newtonsoft.Json by default will attempt to deserialize date strings and use timezone conversion.
You can either turn off Newtonsoft.Json's automatic parsing by setting the DateParseHandling to None and use string
as the model type or have some custom date parsing Newtonsoft.Json converter.
Similar to https://github.com/Azure/azure-cosmos-dotnet-v3/issues/880
@anttikes Did you try customizing Newtonsoft.Json? Any results?
@ealsur No, we didn't try customizing it. As stated in https://github.com/dotnet/efcore/issues/25656 our team decided to wait until the flag will be set by default in EF Core.
It should be noted that the model property's type is string
. That's the weird part. I would've understood this quirky behavior if the type would've been DateTime
or similar.
I'll close this ticket.
This bug was first reported against the dotnet/efcore repository but according to their investigation the problem is in the Cosmos SDK itself.
Describe the bug A string which is formatted to look like a ISO datetime stamp is read back incorrectly from the database. Below is an excerpt from the output of the sample application:
To Reproduce The attached ZIP file contains a solution which utilizes EF Core to write and read back a string which is formatted to look like a ISO datetime stamp.
EFCoreISODatesBug.zip
Expected behavior Since the model uses a plain string as the property's type the expected behavior is that the strings are saved into CosmosDB as-is, and when they are read back they are still exactly the same as before, with no formatting changes or offset calculations whatsoever.
Actual behavior The items are written correctly to the CosmosDB container, and the content of the documents therein corresponds exactly to the expected outcome.
When the items are read back from CosmosDB then the ISODate property is formatted differently, and the time stamp has automatically been adjusted by the UTC offset. On my computer, when I reproduced the issue, the adjustment was done incorrectly as well. This is evident in the excerpt shown above.
Environment summary Output from 'dotnet --info':
Underlying Azure SDK version: 3.12.0 Target framework: .NET 5.0 Operating system: Windows 10 Enterprise (version "10.0.19043 Build 19043") IDE: Visual Studio 2019 version 16.11.1 CosmosDB Emulator version: 2.14.1.0 (08dca53e) Operating system time zone settings: "UTC+02:00 Helsinki, Kyiv, Riga, Sofia, Tallinn, Vilnius"