使用 Dapper 多次执行存储过程?

Execute Stored Procedure multiple times using Dapper?

我希望 Dapper 为每个项目的参数执行一次存储过程。

我正在循环收集以构建参数。而且我不想将执行方法放在循环中。

我怎样才能做到这一点?提前致谢。

public static int SaveData3() 
        {
            var list = new List<Person>();
            var p1 = new Person() { Id = 20, FirstName = "Michiel", LastName = "De Ruyter" };
            var p2 = new Person() { Id = 21, FirstName = "Jacob", LastName = "Jakobsson" };
            list.Add(p1);
            list.Add(p2);

            var queryParameters = new DynamicParameters();
            foreach (var item in list)
            {
                queryParameters.Add("@id", item.Id);
                Log.Information("added {0} as parameter", item.Id);
                queryParameters.Add("@first_name", item.FirstName);
                Log.Information("added {0} as parameter", item.FirstName);
                queryParameters.Add("@last_name", item.LastName);
                Log.Information("added {0} as parameter", item.LastName);
            }

            using (IDbConnection connection = new OracleConnection(GetConnectionString()))
            {
                try
                {
                    var affectedRows = connection.Execute("kwc_test_person_procedure",
                    queryParameters,
                    commandType: CommandType.StoredProcedure
                    );
                    Log.Information("Executed procedure to store data. Rows affected: {0}", affectedRows);
                    return affectedRows;
                }
                catch (Exception ex)
                {
                    Log.Error(ex.Message);
                    Log.Error(ex.StackTrace);
                    throw;
                }

            }
        } 

当我 运行 这样做时,只有最后一个对象 (Jacob) 存储在数据库中。

2021-06-22 13:52:58.225 +02:00 [INF] added 20 as parameter 2021-06-22 13:52:58.287 +02:00 [INF] added Michiel as parameter 2021-06-22 13:52:58.291 +02:00 [INF] added De Ruyter as parameter 2021-06-22 13:52:58.293 +02:00 [INF] added 21 as parameter 2021-06-22 13:52:58.296 +02:00 [INF] added Jacob as parameter 2021-06-22 13:52:58.299 +02:00 [INF] added Jakobsson as parameter 2021-06-22 13:52:58.817 +02:00 [INF] Executed procedure to store data. Rows affected: 1

我也尝试遵循以下“许多”示例: https://dapper-tutorial.net/execute#example---execute-stored-procedure

try
                {
                    var persons = new Person[] {
                    new Person {Id= 41, FirstName = "homeless", LastName="dude"},
                    new Person {Id= 42, FirstName = "hungry", LastName="person"}
                };

                    int rowsAffected = connection.Execute("kwc_test_person_procedure", persons, commandType: CommandType.StoredProcedure);
                    Log.Information("Executed procedure to store data. Rows affected: {0}", rowsAffected);
                    return rowsAffected;
                }

但后来我得到:

ORA-06550 PLS-00306 (wrong number or types of arguments in call ...)

当使用存储过程时,Dapper 看不到签名,因此它假定应添加所有可达的成员。我猜测 Person 具有 other 属性,这是导致此失败的原因。

尝试,然后:

int rowsAffected = connection.Execute("kwc_test_person_procedure",
    persons.Select(x => new { x.Id, x.FirstName, x.LastName }),
    commandType: CommandType.StoredProcedure);

这会创建 just IdFirstNameLastName 的每个人投影,(看起来)应该与存储过程参数。如果需要,您还可以使用此方法为它们指定不同的名称,例如:

    persons.Select(x => new { id = x.Id, fName = x.FirstName, lName = x.LastName }),