当数组参数的值为 null 时无法确定其类型
Cannot determine type of array parameter when its value is null
使用 PostgreSQL 数据库和 Dapper。
这是失败代码的一个最小示例:
await using var connection = new NpgsqlConnection(_configuration.ConnectionString);
return await connection.QueryAsync<Diamond>(
@"SELECT
id,
name
FROM
product
WHERE
@Names IS NULL OR name = ANY(@Names);",
new
{
Names = Names,
});
如果 Names = new { "Tom", "Dick", "Harry" }
.
这会很好用
但是如果 Names = (string[]) null
.
它会失败
我得到的错误是:
Npgsql.PostgresException (0x80004005): 42P18: could not determine data type of parameter
at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
at Dapper.SqlMapper.QueryAsync[T](IDbConnection cnn, Type effectiveType, CommandDefinition command) in /_/Dapper/SqlMapper.Async.cs:line 419
是否可以将 Dapper 中的可为空数组参数发送到 PostgreSQL,或者我是否需要找到解决方法?
@PeterCsala 是正确的。问题是 PostgreSQL 准备查询服务器端,因此在 C# 世界中转换参数没有帮助。铸造必须在 SQL 世界中完成。
工作代码变为:
await using var connection = new NpgsqlConnection(_configuration.ConnectionString);
return await connection.QueryAsync<Diamond>(
@"SELECT
id,
name
FROM
product
WHERE
@Names::text[] IS NULL OR name = ANY(@Names);",
new
{
Names = Names,
});
使用 PostgreSQL 数据库和 Dapper。
这是失败代码的一个最小示例:
await using var connection = new NpgsqlConnection(_configuration.ConnectionString);
return await connection.QueryAsync<Diamond>(
@"SELECT
id,
name
FROM
product
WHERE
@Names IS NULL OR name = ANY(@Names);",
new
{
Names = Names,
});
如果 Names = new { "Tom", "Dick", "Harry" }
.
但是如果 Names = (string[]) null
.
我得到的错误是:
Npgsql.PostgresException (0x80004005): 42P18: could not determine data type of parameter
at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
at Dapper.SqlMapper.QueryAsync[T](IDbConnection cnn, Type effectiveType, CommandDefinition command) in /_/Dapper/SqlMapper.Async.cs:line 419
是否可以将 Dapper 中的可为空数组参数发送到 PostgreSQL,或者我是否需要找到解决方法?
@PeterCsala 是正确的。问题是 PostgreSQL 准备查询服务器端,因此在 C# 世界中转换参数没有帮助。铸造必须在 SQL 世界中完成。
工作代码变为:
await using var connection = new NpgsqlConnection(_configuration.ConnectionString);
return await connection.QueryAsync<Diamond>(
@"SELECT
id,
name
FROM
product
WHERE
@Names::text[] IS NULL OR name = ANY(@Names);",
new
{
Names = Names,
});