Skip to content

NpgsqlDataAdapter problems in current version with domain and array types #6240

@deng0

Description

@deng0

we are trying to update npgsql from version 7.0 to a newer version in our legacy software that still relies on NpgsqlDataAdapter, but we have come across two problems, that prevents us from updating.

To reproduce the problem, create a testdb with the following SQL:

CREATE DOMAIN public.domtest AS smallint;

CREATE TABLE public.testtable
(
    id serial PRIMARY KEY,
    domtest domtest
);

CREATE TABLE public.testtable2
(
    id serial PRIMARY KEY,
    textarr text[] COLLATE pg_catalog."default"
);

And here is the code:

using var dataSource = NpgsqlDataSource.Create(connectionString);

using var connection = dataSource.OpenConnection();

// first problem: Domain Types with NpgsqlCommandBuilder
try
{
    using NpgsqlDataAdapter adapter = new NpgsqlDataAdapter("select * from testtable", connection);

    NpgsqlCommandBuilder builder = new NpgsqlCommandBuilder(adapter)
    {
        ConflictOption = ConflictOption.CompareAllSearchableValues,
        SetAllValues = true
    };

    // the generated commands use 'Unknown' for the domtest parameters
    adapter.InsertCommand = builder.GetInsertCommand();
    adapter.UpdateCommand = builder.GetUpdateCommand();
    adapter.DeleteCommand = builder.GetDeleteCommand();

    using DataTable dataTable = new DataTable();

    // successfully creates the columns (using DataType short for domtest) 
    adapter.Fill(dataTable);

    short sval = 5;

    var newRow = dataTable.NewRow();
    newRow[1] = sval;
    dataTable.Rows.Add(newRow);

    // Update does not work, because InsertCommand stumbles over 'Unknown'.
    // System.InvalidCastException: 'Writing values of 'System.Int16' is not supported for parameters having NpgsqlDbType 'Unknown'.'
    adapter.Update(dataTable);
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}

// second problem: array types with Fill after FillSchema
try
{
    using NpgsqlDataAdapter adapter = new NpgsqlDataAdapter("select * from testtable2", connection);

    using DataTable dataTable = new DataTable();

    // only load the schema
    adapter.FillSchema(dataTable, SchemaType.Source);

    // now we want to load the data, but it should not modify the existing column definitions
    adapter.MissingSchemaAction = MissingSchemaAction.Ignore;
    // it would work with (MissingSchemaAction.AddWithKey, but not with the Add or Ignore)

    // Fill fails, because it has a problem with array type columns
    // System.InvalidOperationException: 'Inconvertible type mismatch between SourceColumn 'textarr' of Array and the DataColumn 'textarr' of String[].'
    adapter.Fill(dataTable);
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}

As far as I can tell the problems occur in all npgsql version >= 8.0.
It would be great if someone can take a look at it.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions