Not sure if this a bug in Npgsql or Dapper or I'm doing it wrong.
I'm trying insert an array of records and return the inserted rows to get the inserted ids. The records are passed as a Postgres array parameter
c# class
public class test
{
public int id { get; set; }
public string name { get; set; }
}
It is mapped to the following table
// id is PK sequence
create table test (
id int,
name text
)
Here's the insertion code
var sql = @"
with t as (
insert into public.test t (id, name)
select i.id, i.name
from unnest(@items) i left outer join t
on i.name = t.name
where i.id is null
returning *
)";
var items = new[]
{
new NpgsqlParameter("items", new []
{
new test() {name = "x"},
new test() {name = "y"},
}
};
// Not using Dapper for command exec as it doesn't support PG composite types
// Only using the RowParser
IList<T> result = new List<test>();
using (var connection = new NpgsqlConnection(...))
{
connection.MapComposite<test>("public.test");
using (NpgsqlCommand cmd = new NpgsqlCommand())
{
cmd.Connection = connection;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.Parameters.Add(parameters);
using (var reader = await cmd.ExecuteReaderAsync())
{
var hasRows = reader.HasRows; // true
var rowParser = reader.GetRowParser<test>(typeof(test)); // <== Dapper parser
result.Add(rowParser(reader)); // <== throws the exception below
}
}
}
result.Add(parser(reader)) throws the following exception (IRowParser is a Dapper interface)
{System.InvalidOperationException: No row is available
at Npgsql.NpgsqlDataReader.CheckRow()
at Npgsql.NpgsqlDataReader.GetValue(Int32 ordinal)
Any ideas what's wrong here?
rowParserhas a value? is yourresultis a class of rows? - Vijunav VastivchrowParseris aIRowParserwhich is a Dapper interface.resultis aList- ubiresultis a list of what? - Vijunav VastivchIList<T> result = new List<test>();- ubivar rowParserif it is a list oftestContent. if not then that is why you got anerrorreturns - Vijunav Vastivch