首页 > 解决方案 > 如何使用 dapper 执行 postgres 函数

问题描述

当我尝试使用 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;

标签: c#postgresqlstored-procedures.net-coredapper

解决方案


尝试以小写形式传递函数参数,即

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>;

这对我有用。


推荐阅读