@@ -39,7 +39,7 @@ public class BatchDelete
3939 /// <summary>The command text template.</summary>
4040 internal const string CommandTextTemplate = @"
4141DELETE
42- FROM A
42+ FROM A {Hint}
4343FROM {TableName} AS A
4444 INNER JOIN ( {Select}
4545 ) AS B ON {PrimaryKeys}
@@ -85,7 +85,7 @@ WHILE @rowAffected IS NULL
8585 OR @rowAffected > 0
8686 BEGIN
8787 DELETE TOP ({Top})
88- FROM A
88+ FROM A {Hint}
8989 FROM {TableName} AS A
9090 INNER JOIN ( {Select}
9191 ) AS B ON {PrimaryKeys}
@@ -113,7 +113,7 @@ WAITFOR DELAY '{Delay}'
113113 END
114114
115115 DELETE TOP ({Top})
116- FROM A
116+ FROM A {Hint}
117117 FROM {TableName} AS A
118118 INNER JOIN ( {Select}
119119 ) AS B ON {PrimaryKeys}
@@ -139,6 +139,10 @@ public BatchDelete()
139139 /// <value>The batch delay interval in milliseconds (The wait time between batch).</value>
140140 public int BatchDelayInterval { get ; set ; }
141141
142+ /// <summary>Gets or sets a value indicating whether the query use table lock.</summary>
143+ /// <value>True if use table lock, false if not.</value>
144+ public bool UseTableLock { get ; set ; }
145+
142146 /// <summary>Gets or sets the DbCommand before being executed.</summary>
143147 /// <value>The DbCommand before being executed.</value>
144148 public Action < DbCommand > Executing { get ; set ; }
@@ -444,7 +448,8 @@ internal DbCommand CreateCommand<T>(ObjectQuery query, SchemaEntityType<T> entit
444448 . Replace ( "{Select}" , querySelect )
445449 . Replace ( "{PrimaryKeys}" , primaryKeys )
446450 . Replace ( "{Top}" , BatchSize . ToString ( ) )
447- . Replace ( "{Delay}" , TimeSpan . FromMilliseconds ( BatchDelayInterval ) . ToString ( @"hh\:mm\:ss\:fff" ) ) ;
451+ . Replace ( "{Delay}" , TimeSpan . FromMilliseconds ( BatchDelayInterval ) . ToString ( @"hh\:mm\:ss\:fff" ) )
452+ . Replace ( "{Hint}" , UseTableLock ? "WITH ( TABLOCK )" : "" ) ;
448453
449454 // CREATE command
450455 command . CommandText = commandTextTemplate ;
@@ -639,7 +644,8 @@ public DbCommand CreateCommand(IQueryable query, IEntityType entity)
639644 . Replace ( "{Select}" , querySelect )
640645 . Replace ( "{PrimaryKeys}" , primaryKeys )
641646 . Replace ( "{Top}" , BatchSize . ToString ( ) )
642- . Replace ( "{Delay}" , TimeSpan . FromMilliseconds ( BatchDelayInterval ) . ToString ( @"hh\:mm\:ss\:fff" ) ) ;
647+ . Replace ( "{Delay}" , TimeSpan . FromMilliseconds ( BatchDelayInterval ) . ToString ( @"hh\:mm\:ss\:fff" ) )
648+ . Replace ( "{Hint}" , UseTableLock ? "WITH ( TABLOCK )" : "" ) ;
643649
644650 // CREATE command
645651 var command = query . GetDbContext ( ) . CreateStoreCommand ( ) ;
0 commit comments