Skip to content

Commit 11a2892

Browse files
Configurable batchsize
1 parent 0481e72 commit 11a2892

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

EntityFramework.Utilities/EntityFramework.Utilities/EFBatchOperation.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public interface IEFBatchOperationBase<TContext, T> where T : class
2020
/// </summary>
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>
23-
void InsertAll(IEnumerable<T> items, DbConnection connection = null);
23+
/// <param name="batchSize">The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default</param>
24+
void InsertAll(IEnumerable<T> items, DbConnection connection = null, int? batchSize = null);
2425
IEFBatchOperationFiltered<TContext, T> Where(Expression<Func<T, bool>> predicate);
2526
}
2627

@@ -66,7 +67,8 @@ public static IEFBatchOperationBase<TContext, T> For<TContext, T>(TContext conte
6667
/// </summary>
6768
/// <param name="items">The items to insert</param>
6869
/// <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>
69-
public void InsertAll(IEnumerable<T> items, DbConnection connection = null)
70+
/// <param name="batchSize">The size of each batch. Default depends on the provider. SqlProvider uses 15000 as default</param>
71+
public void InsertAll(IEnumerable<T> items, DbConnection connection = null, int? batchSize = null)
7072
{
7173
var con = context.Connection as EntityConnection;
7274
if (con == null)
@@ -87,7 +89,7 @@ public void InsertAll(IEnumerable<T> items, DbConnection connection = null)
8789

8890
var properties = tableMapping.PropertyMappings.Select(p => new ColumnMapping { NameInDatabase = p.ColumnName, NameOnObject = p.PropertyName }).ToList();
8991

90-
provider.InsertItems(items, tableMapping.Schema, tableMapping.TableName, properties, connectionToUse);
92+
provider.InsertItems(items, tableMapping.Schema, tableMapping.TableName, properties, connectionToUse, batchSize);
9193
}
9294
else
9395
{

EntityFramework.Utilities/EntityFramework.Utilities/IQueryProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public interface IQueryProvider
1414

1515
string GetDeleteQuery(QueryInformation queryInformation);
1616
string GetUpdateQuery(QueryInformation predicateQueryInfo, QueryInformation modificationQueryInfo);
17-
void InsertItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection);
17+
void InsertItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize);
1818

1919
bool CanHandle(DbConnection storeConnection);
2020

EntityFramework.Utilities/EntityFramework.Utilities/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("0.4.2.0")]
36-
[assembly: AssemblyFileVersion("0.4.2.0")]
35+
[assembly: AssemblyVersion("0.5.0.0")]
36+
[assembly: AssemblyFileVersion("0.5.0.0")]

EntityFramework.Utilities/EntityFramework.Utilities/SqlQueryProvider.cs

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

49-
public void InsertItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection)
49+
public void InsertItems<T>(IEnumerable<T> items, string schema, string tableName, IList<ColumnMapping> properties, DbConnection storeConnection, int? batchSize)
5050
{
5151
using (var reader = new EFDataReader<T>(items, properties))
5252
{
@@ -57,7 +57,7 @@ public void InsertItems<T>(IEnumerable<T> items, string schema, string tableName
5757
}
5858
using (SqlBulkCopy copy = new SqlBulkCopy(con))
5959
{
60-
copy.BatchSize = Math.Min(reader.RecordsAffected, 15000); //default batch size
60+
copy.BatchSize = Math.Min(reader.RecordsAffected, batchSize ?? 15000); //default batch size
6161
if (!string.IsNullOrWhiteSpace(schema))
6262
{
6363
copy.DestinationTableName = string.Format("[{0}].[{1}]", schema, tableName);

0 commit comments

Comments
 (0)