Skip to content

Migrations AddColumn with DateTime type should generate timestamp with timezone #1179

Closed

Description

@RafaelSalguero

Currently the AddColumn method such as:

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.

Activity

  1. rwasef1830 commented on Jun 23, 2016

    @rwasef1830
    Contributor

    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.
  2. RafaelSalguero commented on Jun 23, 2016

    @RafaelSalguero
    Author

    Thank you very much, that will do.

    May I suggest that your solutions are added to the npgsql datetime documentation for future reference?

  3. roji commented on May 14, 2018

    @roji
    Member

    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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions