You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core及依赖包升级后PostgreSQL函数调用失效问题求助

问题解决:升级Npgsql后调用PostgreSQL函数异常

问题根源

PostgreSQL中函数(Function)与存储过程(Procedure)是完全独立的数据库对象,新版本Npgsql驱动严格区分了二者的调用规则:

  • 存储过程需用CALL语句调用,对应CommandType.StoredProcedure
  • 函数需用SELECT语句调用,对应默认的CommandType.Text

旧版本Npgsql对CommandType.StoredProcedure的处理较为宽松,会自动将其转换为函数调用,但新版本不再兼容这种写法,因此触发报错。

解决方案

方法1:使用SELECT语句直接调用函数

修改代码,将CommandText改为标准的PostgreSQL函数调用语法,保持CommandType为默认的Text(无需显式设置):

DbTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadCommitted);

try
{
    using (DbCommand command = connection.CreateCommand())
    {
        command.Transaction = transaction;
        // 用SELECT语法调用函数,注意函数名大小写:若包含大写需用双引号包裹
        command.CommandText = "SELECT public.\"NotableEventUserModeratorJoinOrder\"(@Param1, @Param2, @Param3)";
        
        // 添加参数,占位符需与CommandText中的@ParamX对应
        command.Parameters.Add(new NpgsqlParameter("@Param1", NpgsqlTypes.NpgsqlDbType.Integer) { Value = val1 });
        command.Parameters.Add(new NpgsqlParameter("@Param2", NpgsqlTypes.NpgsqlDbType.Varchar) { Value = val2 });
        command.Parameters.Add(new NpgsqlParameter("@Param3", NpgsqlTypes.NpgsqlDbType.Varchar) { Value = val3 });
        
        var res = command.ExecuteScalar();
        transaction.Commit();
    }
}
catch (Exception ex)
{
    transaction.Rollback();
    throw;
}

方法2:使用命名参数调用(可选,可读性更强)

如果函数参数较多,可使用PostgreSQL的命名参数语法,避免参数顺序错误:

command.CommandText = "SELECT public.\"NotableEventUserModeratorJoinOrder\"(" +
                      "Param1 := @Param1, " +
                      "Param2 := @Param2, " +
                      "Param3 := @Param3)";

关键注意事项

  1. 标识符大小写:PostgreSQL默认会将未加双引号的标识符转为小写,若你的函数名包含大写字母,必须用双引号包裹(如"NotableEventUserModeratorJoinOrder"),否则会提示函数不存在。
  2. 事务处理:保留原有事务逻辑,确保异常时能回滚。
  3. 参数匹配:参数名称需与CommandText中的占位符完全对应,避免因参数不匹配导致的语法错误。

内容的提问来源于stack exchange,提问作者abhishek sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 19:50:29