uriDium
uriDium

Reputation: 13420

How to insert a NULL into a timestamp with timezone column into Postgres using C# client and generic parameter API

I have a column in a table whose type is set to Timestamp with time zone. I am calling a function (whose argument is also of type Timestamp with time zone) to insert rows into the table. I am using the generic parameter API and I need to be able to set the value to NULL. I cannot use

command.Parameters.Add(new NpgsqlParameter<DateTime>("p_responded", null));

I get a compilation error that there is no constructor. So I tried using a nullable DT:

command.Parameters.Add(new NpgsqlParameter<DateTime?>("p_responded", null));

I get the following exception

System.InvalidCastException: Cannot write DateTime with Kind=UTC to PostgreSQL type 'timestamp without time zone', consider using 'timestamp with time zone'. Note that it's not possi ble to mix DateTimes with different Kinds in an array/range. See the Npgsql.EnableLegacyTimestampBehavior AppContext switch to enable legacy behavior.

Upvotes: 0

Views: 1296

Answers (1)

Shay Rojansky
Shay Rojansky

Reputation: 16722

Supporting null with the generic parameter API is tracked in https://github.com/npgsql/npgsql/issues/3679.

In the meantime, use the non-generic parameter API and pass DBNull.Value.

Upvotes: 2

Related Questions