You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Migrations AddColumn with DateTime type should generate timestamp with timezone #1179
AddColumn("public.Table", "Date", c => c.DateTime(nullable: false));
Generates a column of type timestamp without timezone. This causes a lot of problems when the same program uses DateTime of different kinds or when C# is used as a backend and JavaScript as front end, since javascript serialization generates strings with the ISO8061 format with UTC 0 dates, but when reading back the value from the database the readed DateTime has the Unknown kind
This causes that equivalent dates of different kinds and values but for the same moment in time are saved as different values on the database.
If the type timestamptz was used instead, the conversion logic here would gracefully handle different DateTime kinds and serialization between programs that use the normalized ISO 8061 would just work.
This has been discussed to death around here in various issues :-)
The decision to map DateTime to timestamp (without timezone) is to behave the same as SqlClient.
There are 2 solutions:
If you can change the entity, change the property type to DateTimeOffset (and this will give you a timestamptz, but watch out when querying it will come with the local offset, but you can do ToUniversalTime to fix it without any date corruption).
If you cannot change the entity, call this.AddInterceptor from the constructor of your DbConfiguration class and register a subclass of DbCommandInterceptor.
Then you can override various methods that will let you replace the returned DbDataReader instance with a proxy wrapper around it that calls DateTime.SpecifyKind on all returned DateTime instances to force them to return to callers as UTC.
See discussion in #1803. Apart from documentation improvements this will not change - DateTimeOffset is the more natural counterpart to timestamptz, and DateTime maps to timestamp (without time zone).
Activity
rwasef1830 commentedon Jun 23, 2016
This has been discussed to death around here in various issues :-)
The decision to map DateTime to timestamp (without timezone) is to behave the same as SqlClient.
There are 2 solutions:
this.AddInterceptorfrom the constructor of yourDbConfigurationclass and register a subclass ofDbCommandInterceptor.DateTime.SpecifyKindon all returned DateTime instances to force them to return to callers as UTC.RafaelSalguero commentedon Jun 23, 2016
Thank you very much, that will do.
May I suggest that your solutions are added to the npgsql datetime documentation for future reference?
roji commentedon May 14, 2018
See discussion in #1803. Apart from documentation improvements this will not change - DateTimeOffset is the more natural counterpart to
timestamptz, and DateTime maps totimestamp(without time zone).