Skip to content

Commit 5a59372

Browse files
authored
Merge pull request #1 from RedDeathGitHub/master
Added ability to pass options to SqlBulkCopy for bulk insert - option…
2 parents a5abc50 + 3264dda commit 5a59372

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

EntityFramework.Utilities/EntityFramework.Utilities/EFBatchOperation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface IEFBatchOperationBase<TContext, T> where T : class
2121
/// <param name="items">The items to insert</param>
2222
/// <param name="connection">The DbConnection to use for the insert. Only needed when for example a profiler wraps the connection. Then you need to provide a connection of the type the provider use.</param>
2323
/// <param name="batchSize">The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default</param>
24-
void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null) where TEntity : class, T;
24+
void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null, SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default, SqlTransaction transaction = null) where TEntity : class, T;
2525
IEFBatchOperationFiltered<TContext, T> Where(Expression<Func<T, bool>> predicate);
2626

2727

@@ -95,7 +95,7 @@ public static IEFBatchOperationBase<TContext, T> For<TContext, T>(TContext conte
9595
/// <param name="items">The items to insert</param>
9696
/// <param name="connection">The DbConnection to use for the insert. Only needed when for example a profiler wraps the connection. Then you need to provide a connection of the type the provider use.</param>
9797
/// <param name="batchSize">The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default</param>
98-
public void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null) where TEntity : class, T
98+
public void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null, SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default, SqlTransaction transaction = null) where TEntity : class, T
9999
{
100100
var con = context.Connection as EntityConnection;
101101
if (con == null && connection == null)
@@ -126,7 +126,7 @@ public void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connecti
126126
});
127127
}
128128

129-
provider.InsertItems(items, tableMapping.Schema, tableMapping.TableName, properties, connectionToUse, batchSize);
129+
provider.InsertItems(items, tableMapping.Schema, tableMapping.TableName, properties, connectionToUse, batchSize, copyOptions, transaction);
130130
}
131131
else
132132
{

EntityFramework.Utilities/EntityFramework.Utilities/IQueryProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Data.Common;
4+
using System.Data.SqlClient;
45
using System.Linq;
56
using System.Text;
67

@@ -15,7 +16,7 @@ public interface IQueryProvider
1516

1617
string GetDeleteQuery(QueryInformation queryInformation);
1718
string GetUpdateQuery(QueryInformation predicateQueryInfo, QueryInformation modificationQueryInfo);
18-
void InsertItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize);
19+
void InsertItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize, SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default, SqlTransaction transaction = null);
1920
void UpdateItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize, UpdateSpecification<T> updateSpecification);
2021

2122
bool CanHandle(DbConnection storeConnection);

EntityFramework.Utilities/EntityFramework.Utilities/SqlQueryProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public string GetUpdateQuery(QueryInformation predicateQueryInfo, QueryInformati
4747
return string.Format("UPDATE [{0}].[{1}] SET {2} {3}", predicateQueryInfo.Schema, predicateQueryInfo.Table, updateSql, predicateQueryInfo.WhereSql);
4848
}
4949

50-
public void InsertItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize)
50+
public void InsertItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize, SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default, SqlTransaction transaction = null)
5151
{
5252
using (var reader = new EFDataReader<T>(items, properties))
5353
{
@@ -56,7 +56,9 @@ public void InsertItems<T>(IEnumerable<T> items, string schema, string tableName
5656
{
5757
con.Open();
5858
}
59-
using (SqlBulkCopy copy = new SqlBulkCopy(con))
59+
using (var copy = transaction == null
60+
? new SqlBulkCopy(con.ConnectionString, copyOptions)
61+
: new SqlBulkCopy(con, copyOptions, transaction))
6062
{
6163
copy.BatchSize = Math.Min(reader.RecordsAffected, batchSize ?? 15000); //default batch size
6264
if (!string.IsNullOrWhiteSpace(schema))

0 commit comments

Comments
 (0)