public static void BulkInsert <T>(this IQueryable <T> entity, IEnumerable <T> items, IProgressObserver progressObserver) where T : class { using (var connection = new SqlConnection(AppConfiguration.GetConnectionString())) { connection.Open(); var transaction = connection.BeginTransaction(); int batchSize = AppConfiguration.GetResultSaveBatchSize(); int batchCount = items.Count() / batchSize; batchCount += items.Count() % batchSize > 0 ? 1 : 0; using (var bulkCopy = new SqlBulkCopy(connection, SqlBulkCopyOptions.Default, transaction)) { bulkCopy.BulkCopyTimeout = AppConfiguration.GetConnectionTimeout(); for (int i = 0; i < batchCount; i++) { IEnumerable <T> batch = items.Skip(i * batchSize).Take(batchSize); DataTable dataTable = batch.ToDataTable(); bulkCopy.DestinationTableName = dataTable.TableName; bulkCopy.WriteToServer(dataTable); progressObserver.InvokeAction(i + 1, batchCount); } } transaction.Commit(); connection.Close(); } }