您将如何使EF呼叫泛型?

时间:2019-03-27 14:27:12

标签: c# generics entity-framework-core

我有以下示例代码(.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);
        }
    }
}

您如何将其转换为单个通用呼叫?我正在为DbSetModel而苦苦挣扎。

1 个答案:

答案 0 :(得分:1)

不是那么简单吗?

joinPoint.proceed()