我有以下示例代码(.NET Core 2.2):
private void IterateSave(List<TokenMaster> items, int min, int max)
{
int skip = min;
int take = max / 5;
while (skip <= max)
{
try
{
var subItems = items.Skip(skip).Take(take).ToList();
_db.TokenMasters.AddRange(subItems); // <---
_db.SaveChanges();
skip += take;
}
catch (Exception ex)
{
Log.Error(ex, "Error saving this data batch! {RecordCount}", take);
IterateSave(items, skip, take);
}
}
}
private void IterateSave(List<Attributes> items, int min, int max)
{
int skip = min;
int take = max / 5;
while (skip <= max)
{
try
{
var subItems = items.Skip(skip).Take(take).ToList();
_db.Attributes.AddRange(subItems); // <---
_db.SaveChanges();
skip += take;
}
catch (Exception ex)
{
Log.Error(ex, "Error saving this data batch! {RecordCount}", take);
IterateSave(items, skip, take);
}
}
}
您如何将其转换为单个通用呼叫?我正在为DbSet
和Model
而苦苦挣扎。
答案 0 :(得分:1)
不是那么简单吗?
joinPoint.proceed()