我正在对一个表执行
bulk insertion
,并使用dapper在另一个表上进行单个更新。DB
是MySql
。但是我担心表现。我不太确定当通过IEnumerable
时,dapper如何在内部执行多次插入。它是在循环还是其他方式中执行每个execute命令?有没有更好的方法可以执行批量插入而不使用Dapper Plus's
批量插入?
代码:
public bool Add(int inquiryId, int sellerType, IEnumerable<int> cityIds)
{
try
{
string insertQuery = "insert into stockcities (inquiryid, sellertype, cityid) value (@InquiryId, @SellerType, @CityId)";
string table = sellerType == 1 ? "sellinquiries" : "customersellinquiries";
string updateQuery = $"update {table} set isMultiCityStock = true where id = {inquiryId}";
using(var con = ClassifiedMySqlMasterConnection)
{
con.Open();
using(var transaction = con.BeginTransaction())
{
con.Execute(insertQuery,param: cityIds.Select(cityId => new {
InquiryId = inquiryId,
SellerType = sellerType,
CityId = cityId
}), commandType: CommandType.Text, transaction:transaction);
con.Execute(updateQuery,commandType: CommandType.Text, transaction: transaction);
transaction.Commit();
}
return true;
}
}
catch(Exception e)
{
Logger.LogError(e);
}
return false;
}