Search code examples
c#postgresqlnpgsql

NPGSQL Copy nullable Value


I'm trying to insert some data into a Postgres Database via the Copy-Statement.

This a part of the code:

            await using var writer =
                await connection.BeginBinaryImportAsync(
                    "COPY \"SomeTable\" (\"NormalLong\", \"NullableLong\") FROM STDIN (FORMAT BINARY)");

            foreach (var t in batch)
            {
                await writer.StartRowAsync();
                await writer.WriteAsync(t.NormalLong, NpgsqlDbType.BigInt);
                await writer.WriteAsync(t.NullableLong, NpgsqlDbType.Bigint);
            }
public class SampleClass {
  public long NormalLong { get; set; }
  public long? NullableLong { get; set; }
}

At the time of execution, both Values on t are set to some numeric value, but the Value of Type long? always results in this error:

System.NullReferenceException: Object reference not set to an instance of an object.

Is there a way to deal with this, without changing the Type, since it sometimes needs to be null?


Solution

  • While the solution provided by CKK works, it results in a warning in my IDE, which would be ok.

    I went with another solution and write a helper class which contains the following code for each data-type I need:

        public static async Task WriteAsync(NpgsqlBinaryImporter writer, long value, NpgsqlDbType type)
        {
            await writer.WriteAsync(value, type);
        }
    
        public static async Task WriteAsync(NpgsqlBinaryImporter writer, long? value, NpgsqlDbType type)
        {
            if (value.HasValue) await writer.WriteAsync(value.Value, type);
            else await writer.WriteNullAsync();
        }