简体   繁体   English

将 dapper 类传递给 postgres 存储过程

[英]Pass dapper class to postgres stored procedure

I have a stored procedure:我有一个存储过程:

CREATE OR REPLACE PROCEDURE insert_participant(j json)
LANGUAGE plpgsql    
AS $$
BEGIN
    INSERT INTO participant (procedure_id,lot_id)SELECT procedure_id,lot_id  FROM json_populate_record(null::participant, j);
    COMMIT;
END;
$$;

And a class:还有一个类:

    public class Participant : BaseEntity
    {
        [Key]
        public int Procedure_id{ get; set; }
        public int Lot_id { get; set; }      
    }

I'm trying the following:我正在尝试以下操作:

        public void Add(Participant item)
        {
            using (IDbConnection dbConnection = Connection)
            {
                dbConnection.Open();
                dbConnection.Execute("insert_participant", item, commandType: CommandType.StoredProcedure);
            }
        }

But it fails and tells that there is no stored procedure with these paremeters.但它失败并告诉这些参数没有存储过程。

What kind of parameter should I define when creating stored procedure in order for this to work?在创建存储过程时我应该定义什么样的参数才能使其工作?

Dapper is expecting to send parameters which match to the declared properties on the type - so in this case it is going to want to send Lot_id and Procedure_id . Dapper 期望发送与类型上声明的属性匹配的参数 - 因此在这种情况下,它将想要发送Lot_idProcedure_id It looks like you want to send the entire object as JSON instead, in which case you probably want:看起来您想将整个对象作为 JSON 发送,在这种情况下,您可能需要:

var json = YourChoiceOfJsonSerializeAPI(item);
// possibly: var json = JsonConvert.SerializeObject(item);
dbConnection.Execute("insert_participant", new { json },
    commandType: CommandType.StoredProcedure);

This now has one member called json , so one parameter named json will be added.现在有一个名为json成员,因此将添加一个名为json参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM