Skip to content

Support writing Nullable<T> in COPY binary importer #1965

@wiktor-golonka

Description

@wiktor-golonka

Steps to reproduce

using (var loader = conn.BeginBinaryImport(query))
{
    foreach (var price in items)
    {
        loader.StartRow();
        loader.Write(price.Id, NpgsqlDbType.Integer);
        loader.Write(price.Date, NpgsqlDbType.Date);
        loader.Write(price.Value, NpgsqlDbType.Numeric);   <--- exception here
    }

    loader.Complete();
}

public struct Price
{
    public int Id { get; set; }
    public DateTime Date { get; set; }
    public decimal? Value { get; set; }
}

The issue

After updating npgsql to 4.0.0:
When type of Value is nullable decimal I'm getting an exception while using binary import.
`Value' = 627.99M.

Exception message:
System.InvalidCastException: 'Can't write CLR type System.Nullable`1[System.Decimal] to database type numeric'
Stack trace:
   at Npgsql.TypeHandling.NpgsqlSimpleTypeHandler`1.ValidateAndGetLength[TAny](TAny value, NpgsqlLengthCache& lengthCache, NpgsqlParameter parameter) in C:\projects\npgsql\src\Npgsql\TypeHandling\NpgsqlSimpleTypeHandler.cs:line 171
   at Npgsql.NpgsqlParameter`1.ValidateAndGetLength() in C:\projects\npgsql\src\Npgsql\NpgsqlParameter`.cs:line 89
   at Npgsql.NpgsqlBinaryImporter.Write[T](T value, NpgsqlParameter param) in C:\projects\npgsql\src\Npgsql\NpgsqlBinaryImporter.cs:line 244
   at Npgsql.NpgsqlBinaryImporter.Write[T](T value, NpgsqlDbType npgsqlDbType) in C:\projects\npgsql\src\Npgsql\NpgsqlBinaryImporter.cs:line 189

Further technical details

Npgsql version: 4.0.0
PostgreSQL version: 10
Operating system: W10 64bit

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions