Skip to content

Commit

Permalink
Merge pull request #1 from RedDeathGitHub/master
Browse files Browse the repository at this point in the history
Added ability to pass options to SqlBulkCopy for bulk insert - option…
  • Loading branch information
RudeySH authored Feb 8, 2018
2 parents a5abc50 + 3264dda commit a66538c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public interface IEFBatchOperationBase<TContext, T> where T : class
/// <param name="items">The items to insert</param>
/// <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>
/// <param name="batchSize">The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default</param>
void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null) where TEntity : class, T;
void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null, SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default, SqlTransaction transaction = null) where TEntity : class, T;
IEFBatchOperationFiltered<TContext, T> Where(Expression<Func<T, bool>> predicate);


Expand Down Expand Up @@ -95,7 +95,7 @@ public static IEFBatchOperationBase<TContext, T> For<TContext, T>(TContext conte
/// <param name="items">The items to insert</param>
/// <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>
/// <param name="batchSize">The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default</param>
public void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null) where TEntity : class, T
public void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connection = null, int? batchSize = null, SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default, SqlTransaction transaction = null) where TEntity : class, T
{
var con = context.Connection as EntityConnection;
if (con == null && connection == null)
Expand Down Expand Up @@ -126,7 +126,7 @@ public void InsertAll<TEntity>(IEnumerable<TEntity> items, DbConnection connecti
});
}

provider.InsertItems(items, tableMapping.Schema, tableMapping.TableName, properties, connectionToUse, batchSize);
provider.InsertItems(items, tableMapping.Schema, tableMapping.TableName, properties, connectionToUse, batchSize, copyOptions, transaction);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

Expand All @@ -15,7 +16,7 @@ public interface IQueryProvider

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

bool CanHandle(DbConnection storeConnection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public string GetUpdateQuery(QueryInformation predicateQueryInfo, QueryInformati
return string.Format("UPDATE [{0}].[{1}] SET {2} {3}", predicateQueryInfo.Schema, predicateQueryInfo.Table, updateSql, predicateQueryInfo.WhereSql);
}

public void InsertItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize)
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)
{
using (var reader = new EFDataReader<T>(items, properties))
{
Expand All @@ -56,7 +56,9 @@ public void InsertItems<T>(IEnumerable<T> items, string schema, string tableName
{
con.Open();
}
using (SqlBulkCopy copy = new SqlBulkCopy(con))
using (var copy = transaction == null
? new SqlBulkCopy(con.ConnectionString, copyOptions)
: new SqlBulkCopy(con, copyOptions, transaction))
{
copy.BatchSize = Math.Min(reader.RecordsAffected, batchSize ?? 15000); //default batch size
if (!string.IsNullOrWhiteSpace(schema))
Expand Down

0 comments on commit a66538c

Please sign in to comment.