Skip to content

Npgsql defaults to parameter type TimespanTz for all DateTime values regardles of DateTimeKind #715

@GoranSiska

Description

@GoranSiska

Using Npgsql (v8.0.2) with Npgsql.EnableLegacyTimestampBehavior switch set to false. When adding parameter to the command, NPoco maps the CLR type DateTime to DbType.DateTime. When NpgsqlParameter.DbType is set to DbType.DateTime it internally sets its NpgsqlDbType to NpgsqlTypes.NpgsqlDbType.TimestampTz, regardless of the DateTime value Kind. When using DateTimeKind.Unspecified for timestamp (with no timezone) columns, this causes an exception:

System.ArgumentException : Cannot write DateTime with Kind=Unspecified to PostgreSQL type 'timestamp with time zone', only UTC is supported. Note that it's not possible to mix DateTimes with different Kinds in an array, range, or multirange. (Parameter 'value')

Is there a workaround? Currently the only place to handle this is in the SetParameterValue method which can not be extended. Additionally this method already contains some code that seems to belong to DatabaseType classes (eg. "SqlCeParameter" handling). Perhaps this metod or a part of it should be moved to DatabaseType? DatabaseType.LookupDbType method is not usable as it only takes type as a parameter while this distinction requires inspection of the DateTime value Kind, and in any case it only sets the DbType which is correctly identified as DateTime.

So the correct behavior would be to map:
DateTimeKind.Unspecified to NpgsqlTypes.NpgsqlDbType.Timestamp
DateTimeKind.Local to NpgsqlTypes.NpgsqlDbType.Timestamp
DateTimeKind.Utc to NpgsqlTypes.NpgsqlDbType.TimestampTz

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions