【问题标题】:How to execute postgres function using dapper如何使用 dapper 执行 postgres 函数
【发布时间】:2019-06-20 12:20:23
【问题描述】:

当我尝试使用 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# postgresql stored-procedures .net-core dapper


【解决方案1】:

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

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

这对我有用。

【讨论】:

  • 参数名称的用户小写,这对我也有用。
【解决方案2】:

我希望您需要指定函数的参数、架构并且结果应该匹配。以下可能会起作用,之后您可以替换返回类型以查看它是否正确映射。

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,我尝试了这种格式,但我收到了相同的错误消息。顺便说一下,模式名称是公开的。为此,我添加了公共前缀。
  • 您是否连接到正确的数据库,如果您在 Sql manager 中执行该语句,那么它是否有效?您是否授予权限为用户 ex 执行此查询:docs.microsoft.com/en-us/sql/t-sql/statements/…
【解决方案3】:

我认为这是因为您创建了函数Availability_List,其中AL 大写。但是随后您将使用所有小写字母调用该函数。我已经遇到过这个问题,在一个小案例中使用所有。这样会更好...

【讨论】:

    猜你喜欢
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 2019-07-29
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多