如何使用Dapper执行Postgres函数

4

当我尝试使用dapper调用postgre函数时,出现了错误。我做错了什么?如果您能帮忙解决,我将不胜感激。

错误信息:

 availability_list(facilityId => integer, startDate => timestamp without time zone, endDate => timestamp without time zone) does not exist"

使用Dapper调用Postgre函数:
var func = "public.availability_list";

var result = db.Query<ReportResponse>(
            sql: func,
            param: new { facilityId = request.FacilityId, startDate = 
            DateTime.Now, endDate = DateTime.Now },
            commandType: CommandType.StoredProcedure, 
            commandTimeout: 900) as List<ReportResponse>;

我的Postgre函数:

CREATE FUNCTION Availability_List(facilityId int, startDate date, endDate date)
RETURNS report_type[] 
AS 
$$

DECLARE
result_record report_type[];

BEGIN

result_record := array(  
SELECT...
);

RETURN result_record;

 END $$ LANGUAGE plpgsql;

我有同样的问题,使用小写参数名称对我有用。 - Artiom
3个回答

2
尝试以小写形式传递函数参数,即
var result = db.Query<ReportResponse>(
            sql: func,
            param: new { facilityid = request.FacilityId, startdate = 
            DateTime.Now, enddate = DateTime.Now },
            commandType: CommandType.StoredProcedure, 
            commandTimeout: 900) as List<ReportResponse>;

那对我有用。


1
用户将参数名称改为小写,这对我也起作用了。 - Artiom

0
我认为这是因为您创建了函数Availability_List,其中AL是大写的。但是,您在调用函数时使用了全部小写。我已经遇到过这个问题,建议您全部使用小写字母。这样会更好...

0
我认为您需要指定参数、函数模式,并确保结果匹配。可能以下内容可以起作用,之后您可以替换返回类型以查看是否正确映射。
var result = _connection.Query<dynamic>(
    "SELECT dbo.Availability_List(@facilityId, @startDate, @endDate)", 
    new { 
        facilityId = request.FacilityId, 
        startDate = DateTime.Now, 
        endDate = DateTime.Now 
    },
    commandType: CommandType.Text,
    commandTimeout: 900);

嗨,Margus,我尝试了这种格式,但是我收到了相同的错误消息。顺便说一下,模式名称是public。因此,我添加了public前缀。 - burak ramazan
你连接到正确的数据库了吗?如果在Sql管理器中执行语句,它是否有效?您是否授予用户执行此查询的权限,例如:https://learn.microsoft.com/en-us/sql/t-sql/statements/grant-object-permissions-transact-sql?view=sql-server-2017 - Margus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接