7

我从 Dapper 官方文档中看到了 QueryMultiple,如下所示,很方便!

     var sql = @"
select * from Customers where CustomerId = @id
select * from Orders where CustomerId = @id
select * from Returns where CustomerId = @id";

using (var multi = connection.QueryMultiple(sql, new {id=selectedId}))
{
   var customer = multi.Read<Customer>().Single();
   var orders = multi.Read<Order>().ToList();
   var returns = multi.Read<Return>().ToList();
   ...
}

现在,当我从父表中删除记录时,我想从子表中删除相关记录。Dapper 能适应吗?它看起来如下。

 var sql = @"delete from tb_role where roleid=@ID
                            delete from tb_rolepermission where roleid=@ID
                            delete from tb_userrole where roleid=@ID
        ";
        var param = new { ID=id };

 connection.EXECUTEMultiple(sql, param).......... 

任何帮助将不胜感激!

4

1 回答 1

5

是的,您可以简单地调用 connection.Execute 并且它已经允许您尝试执行多个命令,就像 SqlCommand 上允许的 ExecuteQuery 一样,这就是 Dapper 无论如何都在调用的所有命令。

于 2012-10-05T06:07:43.590 回答