diff --git a/EntityFramework.Utilities/EntityFramework.Utilities/EFBatchOperation.cs b/EntityFramework.Utilities/EntityFramework.Utilities/EFBatchOperation.cs index 72fa98c..5975391 100644 --- a/EntityFramework.Utilities/EntityFramework.Utilities/EFBatchOperation.cs +++ b/EntityFramework.Utilities/EntityFramework.Utilities/EFBatchOperation.cs @@ -21,7 +21,7 @@ public interface IEFBatchOperationBase where T : class /// The items to insert /// 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. /// The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default - void InsertAll(IEnumerable items, DbConnection connection = null, int? batchSize = null) where TEntity : class, T; + void InsertAll(IEnumerable items, DbConnection connection = null, int? batchSize = null, SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default, SqlTransaction transaction = null) where TEntity : class, T; IEFBatchOperationFiltered Where(Expression> predicate); @@ -95,7 +95,7 @@ public static IEFBatchOperationBase For(TContext conte /// The items to insert /// 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. /// The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default - public void InsertAll(IEnumerable items, DbConnection connection = null, int? batchSize = null) where TEntity : class, T + public void InsertAll(IEnumerable 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) @@ -126,7 +126,7 @@ public void InsertAll(IEnumerable 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 { diff --git a/EntityFramework.Utilities/EntityFramework.Utilities/IQueryProvider.cs b/EntityFramework.Utilities/EntityFramework.Utilities/IQueryProvider.cs index e0af4d9..956d48f 100644 --- a/EntityFramework.Utilities/EntityFramework.Utilities/IQueryProvider.cs +++ b/EntityFramework.Utilities/EntityFramework.Utilities/IQueryProvider.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Data.Common; +using System.Data.SqlClient; using System.Linq; using System.Text; @@ -15,7 +16,7 @@ public interface IQueryProvider string GetDeleteQuery(QueryInformation queryInformation); string GetUpdateQuery(QueryInformation predicateQueryInfo, QueryInformation modificationQueryInfo); - void InsertItems(IEnumerable items, string schema, string tableName, IList properties, DbConnection storeConnection, int? batchSize); + void InsertItems(IEnumerable items, string schema, string tableName, IList properties, DbConnection storeConnection, int? batchSize, SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default, SqlTransaction transaction = null); void UpdateItems(IEnumerable items, string schema, string tableName, IList properties, DbConnection storeConnection, int? batchSize, UpdateSpecification updateSpecification); bool CanHandle(DbConnection storeConnection); diff --git a/EntityFramework.Utilities/EntityFramework.Utilities/SqlQueryProvider.cs b/EntityFramework.Utilities/EntityFramework.Utilities/SqlQueryProvider.cs index ca85d23..5b51fcf 100644 --- a/EntityFramework.Utilities/EntityFramework.Utilities/SqlQueryProvider.cs +++ b/EntityFramework.Utilities/EntityFramework.Utilities/SqlQueryProvider.cs @@ -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(IEnumerable items, string schema, string tableName, IList properties, DbConnection storeConnection, int? batchSize) + public void InsertItems(IEnumerable items, string schema, string tableName, IList properties, DbConnection storeConnection, int? batchSize, SqlBulkCopyOptions copyOptions = SqlBulkCopyOptions.Default, SqlTransaction transaction = null) { using (var reader = new EFDataReader(items, properties)) { @@ -56,7 +56,9 @@ public void InsertItems(IEnumerable 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))