Skip to content

Commit 471bfb6

Browse files
committed
- 优化 EnableCascadeSave 级联保存执行逻辑,提升性能;
1 parent 3ce2193 commit 471bfb6

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

FreeSql.DbContext/DbSet/DbSetAsync.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,18 +328,29 @@ async Task AddOrUpdateNavigateAsync(TEntity item, bool isAdd, string propertyNam
328328
}
329329
break;
330330
case Internal.Model.TableRefType.OneToMany:
331-
var addlist = isAdd ? new List<object>() : null;
331+
var addList = new List<object>();
332+
var addOrUpdateList = new List<object>();
332333
foreach (var propValItem in propValEach)
333334
{
334335
for (var colidx = 0; colidx < tref.Columns.Count; colidx++)
335336
{
336337
var val = FreeSql.Internal.Utils.GetDataReaderValue(tref.RefColumns[colidx].CsType, _db.OrmOriginal.GetEntityValueWithPropertyName(_table.Type, item, tref.Columns[colidx].CsName));
337338
_db.OrmOriginal.SetEntityValueWithPropertyName(tref.RefEntityType, propValItem, tref.RefColumns[colidx].CsName, val);
338339
}
339-
if (isAdd) addlist.Add(propValItem);
340-
else await refSet.AddOrUpdateAsync(propValItem, cancellationToken);
340+
if (isAdd) addList.Add(propValItem);
341+
else
342+
{
343+
var flagExists = refSet.ExistsInStates(propValItem);
344+
if (flagExists == null) addList.Add(propValItem); //自增/Guid
345+
else addOrUpdateList.Add(propValItem); //统一状态管理
346+
}
347+
}
348+
if (addList.Any()) await refSet.AddRangeAsync(addList, cancellationToken);
349+
if (addOrUpdateList.Any())
350+
{
351+
var existsList = await refSet.Select.WhereDynamic(addOrUpdateList).ToListAsync(false, cancellationToken);
352+
foreach (var aouItem in addOrUpdateList) await refSet.AddOrUpdateAsync(aouItem, cancellationToken);
341353
}
342-
if (isAdd) await refSet.AddRangeAsync(addlist, cancellationToken);
343354
break;
344355
}
345356
};

FreeSql.DbContext/DbSet/DbSetSync.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,18 +339,29 @@ void AddOrUpdateNavigate(TEntity item, bool isAdd, string propertyName)
339339
}
340340
break;
341341
case Internal.Model.TableRefType.OneToMany:
342-
var addlist = isAdd ? new List<object>() : null;
342+
var addList = new List<object>();
343+
var addOrUpdateList = new List<object>();
343344
foreach (var propValItem in propValEach)
344345
{
345346
for (var colidx = 0; colidx < tref.Columns.Count; colidx++)
346347
{
347348
var val = FreeSql.Internal.Utils.GetDataReaderValue(tref.RefColumns[colidx].CsType, _db.OrmOriginal.GetEntityValueWithPropertyName(_table.Type, item, tref.Columns[colidx].CsName));
348349
_db.OrmOriginal.SetEntityValueWithPropertyName(tref.RefEntityType, propValItem, tref.RefColumns[colidx].CsName, val);
349350
}
350-
if (isAdd) addlist.Add(propValItem);
351-
else refSet.AddOrUpdate(propValItem);
351+
if (isAdd) addList.Add(propValItem);
352+
else
353+
{
354+
var flagExists = refSet.ExistsInStates(propValItem);
355+
if (flagExists == null) addList.Add(propValItem); //自增/Guid
356+
else addOrUpdateList.Add(propValItem); //统一状态管理
357+
}
358+
}
359+
if (addList.Any()) refSet.AddRange(addList);
360+
if (addOrUpdateList.Any())
361+
{
362+
var existsList = refSet.Select.WhereDynamic(addOrUpdateList).ToList();
363+
foreach (var aouItem in addOrUpdateList) refSet.AddOrUpdate(aouItem);
352364
}
353-
if (isAdd) refSet.AddRange(addlist);
354365
break;
355366
}
356367
};

0 commit comments

Comments
 (0)