/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="context"></param> /// <param name="entities"></param> /// <param name="options"></param> public static void BulkInsert <T>(this DbContext context, IEnumerable <T> entities, BulkInsertOptions options) { var bulkInsert = ProviderFactory.Get(context); bulkInsert.Options = options; bulkInsert.Run(entities); }
public static async Task BulkInsertBuffered <T>(this DbContext context, IEnumerable <T> entities, BulkInsertOptions options) { var bulkInsert = ProviderFactory.Get(context); bulkInsert.Options = options; var mutualExclusionLock = new object(); var t1 = Task.Run(async() => await BulkInsertBufferedBody <T>(bulkInsert, entities, options, mutualExclusionLock)); var t2 = Task.Run(async() => await BulkInsertBufferedBody <T>(bulkInsert, entities, options, mutualExclusionLock)); await Task.WhenAll(t1, t2); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="context"></param> /// <param name="entities"></param> /// <param name="options"></param> public static Task BulkInsertAsync <T>(this DbContext context, IEnumerable <T> entities, BulkInsertOptions options) { var bulkInsert = ProviderFactory.Get(context); bulkInsert.Options = options; return(bulkInsert.RunAsync(entities)); }
public static async Task BulkInsertBufferedBody <T>(IEfBulkInsertProvider bulkInsert, IEnumerable <T> entities, BulkInsertOptions options, object mutualExclusionLock) { while (true) { var buffer = new List <T>(); lock (mutualExclusionLock) { foreach (var item in entities) { buffer.Add(item); if (buffer.Count == options.BatchSize) { break; } } } if (!buffer.Any()) { return; } await BulkInsertAsync(bulkInsert.Context, buffer, options); } }