简体   繁体   中英

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 . It looks like you want to send the entire object as JSON instead, in which case you probably want:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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