-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSqlServerBulkInsertProvider.cs
More file actions
94 lines (78 loc) · 3.05 KB
/
SqlServerBulkInsertProvider.cs
File metadata and controls
94 lines (78 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using JetBrains.Annotations;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Logging;
using PhenX.EntityFrameworkCore.BulkInsert.Metadata;
namespace PhenX.EntityFrameworkCore.BulkInsert.SqlServer;
[UsedImplicitly]
internal class SqlServerBulkInsertProvider(ILogger<SqlServerBulkInsertProvider>? logger) : BulkInsertProviderBase<SqlServerDialectBuilder, SqlServerBulkInsertOptions>(logger)
{
//language=sql
/// <inheritdoc />
protected override string AddTableCopyBulkInsertId => $"ALTER TABLE {{0}} ADD {BulkInsertId} INT IDENTITY PRIMARY KEY;";
/// <inheritdoc />
protected override string GetTempTableName(string tableName) => $"#_temp_bulk_insert_{tableName}_{Helpers.RandomString(6)}";
protected override SqlServerBulkInsertOptions CreateDefaultOptions() => new()
{
BatchSize = 50_000,
Converters = [SqlServerGeometryConverter.Instance]
};
/// <inheritdoc />
protected override async Task BulkInsert<T>(
bool sync,
DbContext context,
TableMetadata tableInfo,
IEnumerable<T> entities,
string tableName,
IReadOnlyList<ColumnMetadata> columns,
SqlServerBulkInsertOptions options,
CancellationToken ctk)
{
var connection = (SqlConnection) context.Database.GetDbConnection();
var sqlTransaction = context.Database.CurrentTransaction!.GetDbTransaction() as SqlTransaction;
using var bulkCopy = new SqlBulkCopy(connection, options.CopyOptions, sqlTransaction);
bulkCopy.DestinationTableName = tableName;
bulkCopy.BatchSize = options.BatchSize;
bulkCopy.BulkCopyTimeout = options.GetCopyTimeoutInSeconds();
bulkCopy.EnableStreaming = options.EnableStreaming;
// Handle progress notifications
if (options is { NotifyProgressAfter: not null, OnProgress: not null })
{
bulkCopy.NotifyAfter = options.NotifyProgressAfter.Value;
bulkCopy.SqlRowsCopied += (sender, e) =>
{
options.OnProgress(e.RowsCopied);
if (ctk.IsCancellationRequested)
{
e.Abort = true;
}
};
}
// If no progress notification is set, we still need to handle cancellation.
else
{
bulkCopy.SqlRowsCopied += (sender, e) =>
{
if (ctk.IsCancellationRequested)
{
e.Abort = true;
}
};
}
foreach (var column in columns)
{
bulkCopy.ColumnMappings.Add(column.PropertyName, column.ColumnName);
}
var dataReader = new EnumerableDataReader<T>(entities, columns, options);
if (sync)
{
// ReSharper disable once MethodHasAsyncOverloadWithCancellation
bulkCopy.WriteToServer(dataReader);
}
else
{
await bulkCopy.WriteToServerAsync(dataReader, ctk);
}
}
}