Optimized code

This commit is contained in:
sunkaixuan
2024-01-09 11:30:43 +08:00
parent 8640c8af7d
commit a020ae7bf8

View File

@@ -209,7 +209,9 @@ namespace SqlSugar
private async Task<int> _BulkMerge(List<T> datas, string[] updateColumns, string[] whereColumns)
{
Begin(datas, false,true);
try
{
Begin(datas, false, true);
Check.Exception(whereColumns == null || whereColumns.Count() == 0, "where columns count=0 or need primary key");
Check.Exception(whereColumns == null || whereColumns.Count() == 0, "where columns count=0 or need primary key");
var isAuto = this.context.CurrentConnectionConfig.IsAutoCloseConnection;
@@ -222,24 +224,32 @@ namespace SqlSugar
await buider.CreateTempAsync<T>(dt);
await buider.ExecuteBulkCopyAsync(dt);
}
var result = await buider.Merge(GetTableName(),dt, this.entityInfo,whereColumns,updateColumns, datas);
var result = await buider.Merge(GetTableName(), dt, this.entityInfo, whereColumns, updateColumns, datas);
//var queryTemp = this.context.Queryable<T>().AS(dt.TableName).ToList();//test
//var result = await buider.UpdateByTempAsync(GetTableName(), dt.TableName, updateColumns, whereColumns);
if (buider?.DbFastestProperties?.IsMerge == true&&this.context.CurrentConnectionConfig.DbType != DbType.Sqlite)
if (buider?.DbFastestProperties?.IsMerge == true && this.context.CurrentConnectionConfig.DbType != DbType.Sqlite)
{
this.context.DbMaintenance.DropTable(dt.TableName);
}
this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto;
buider.CloseDb();
End(datas, false,true);
End(datas, false, true);
return result;
}
catch (Exception)
{
this.context.Close();
throw;
}
}
#endregion
#region Core
private async Task<int> _BulkUpdate(List<T> datas, string[] whereColumns, string[] updateColumns)
{
Begin(datas,false);
try
{
Begin(datas, false);
Check.Exception(whereColumns == null || whereColumns.Count() == 0, "where columns count=0 or need primary key");
Check.Exception(updateColumns == null || updateColumns.Count() == 0, "set columns count=0");
var isAuto = this.context.CurrentConnectionConfig.IsAutoCloseConnection;
@@ -261,6 +271,12 @@ namespace SqlSugar
End(datas, false);
return result;
}
catch (Exception)
{
this.context.Close();
throw;
}
}
private void ActionIgnoreColums(string[] whereColumns, string[] updateColumns, DataTable dt,bool IsActionUpdateColumns)
{