55
66namespace EfCore . BulkOperations ;
77
8-
98/// <summary>
109/// Provides a set of static methods for performing efficient bulk operations (insert, update, delete, merge)
1110/// on entities using Entity Framework Core.
@@ -32,7 +31,7 @@ internal async static Task<int> BulkInsertAsync<T>(DbContext dbContext,
3231 optionFactory ? . Invoke ( option ) ;
3332
3433 var batches = BulkCommand . GenerateInsertBatches ( dbContext , items , option ) ;
35- await BulkExecuteAsync ( dbContext , batches , transaction , cancellationToken ) ;
34+ await BulkExecuteAsync ( dbContext , batches , option . CommandTimeout , transaction , cancellationToken ) ;
3635 return batches . Sum ( x => x . SuccessCount ) ?? 0 ;
3736 }
3837
@@ -56,7 +55,7 @@ internal async static Task<int> BulkUpdateAsync<T>(DbContext dbContext,
5655 optionFactory ? . Invoke ( option ) ;
5756
5857 var batches = BulkCommand . GenerateUpdateBatches ( dbContext , items , option ) ;
59- await BulkExecuteAsync ( dbContext , batches , transaction , cancellationToken ) ;
58+ await BulkExecuteAsync ( dbContext , batches , option . CommandTimeout , transaction , cancellationToken ) ;
6059 return batches . Sum ( x => x . SuccessCount ) ?? 0 ;
6160 }
6261
@@ -80,7 +79,7 @@ internal async static Task<int> BulkDeleteAsync<T>(DbContext dbContext,
8079 optionFactory ? . Invoke ( option ) ;
8180
8281 var batches = BulkCommand . GenerateDeleteBatches ( dbContext , items , option ) ;
83- await BulkExecuteAsync ( dbContext , batches , transaction , cancellationToken ) ;
82+ await BulkExecuteAsync ( dbContext , batches , option . CommandTimeout , transaction , cancellationToken ) ;
8483 return batches . Sum ( x => x . SuccessCount ) ?? 0 ;
8584 }
8685
@@ -106,7 +105,7 @@ internal async static Task<int> BulkMergeAsync<T>(DbContext dbContext,
106105 optionFactory ? . Invoke ( option ) ;
107106
108107 var batches = BulkCommand . GenerateMergeBatches ( dbContext , items , option ) ;
109- await BulkExecuteAsync ( dbContext , batches , transaction , cancellationToken ) ;
108+ await BulkExecuteAsync ( dbContext , batches , option . CommandTimeout , transaction , cancellationToken ) ;
110109 return batches . Sum ( x => x . SuccessCount ) ?? 0 ;
111110 }
112111
@@ -119,6 +118,7 @@ internal async static Task<int> BulkMergeAsync<T>(DbContext dbContext,
119118 private static async Task BulkExecuteAsync (
120119 DbContext dbContext ,
121120 IEnumerable < BatchData > batches ,
121+ int ? commandTimeout = null ,
122122 DbTransaction ? externalTransaction = null ,
123123 CancellationToken ? cancellationToken = null )
124124 {
@@ -128,7 +128,7 @@ private static async Task BulkExecuteAsync(
128128 try
129129 {
130130 foreach ( var batch in batches )
131- await ExecuteBatchDataAsync ( batch , connection , transaction , cancellationToken ) ;
131+ await ExecuteBatchDataAsync ( batch , connection , commandTimeout , transaction , cancellationToken ) ;
132132
133133 if ( externalTransaction is null ) await transaction . CommitAsync ( cancellationToken ?? default ) ;
134134 }
@@ -150,16 +150,23 @@ private static async Task BulkExecuteAsync(
150150 /// <summary>
151151 /// Executes a single SQL batch asynchronously.
152152 /// </summary>
153+ /// <param name="batch">DbConnection</param>
154+ /// <param name="connection">DbConnection</param>
155+ /// <param name="commandTimeout">commandTimeout</param>
156+ /// <param name="dbTransaction">dbTransaction</param>
157+ /// <param name="cancellationToken">cancellationToken</param>
153158 private static async Task ExecuteBatchDataAsync (
154159 BatchData batch ,
155160 DbConnection connection ,
161+ int ? commandTimeout ,
156162 DbTransaction ? dbTransaction ,
157163 CancellationToken ? cancellationToken = null )
158164 {
159165 await using var command = connection . CreateCommand ( ) ;
160166 if ( command . Connection is null ) throw new ArgumentException ( "Command.Connection is null" ) ;
161167 if ( dbTransaction is not null ) command . Transaction = dbTransaction ;
162168 command . CommandText = batch . Sql . ToString ( ) ;
169+ if ( commandTimeout is not null ) command . CommandTimeout = commandTimeout . Value ;
163170
164171 batch . Parameters . ToList ( ) . ForEach ( p =>
165172 {
0 commit comments