@@ -100,34 +100,24 @@ public static Task<int> InsertIntoAsync<T>(this SqlServerDatabase database, stri
100100 /// <param name="database">SQL Server database which the data have to be deleted.</param>
101101 public static void ClearAllData ( this SqlServerDatabase database )
102102 {
103- database . ExecuteNonQuery ( "EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'" ) ;
104-
105- database . ExecuteNonQuery ( "EXEC sp_msforeachtable 'SET QUOTED_IDENTIFIER ON; DELETE FROM ?'" ) ;
106-
107- // Re-initialize the seed of the IDENTITY columns.
108- // For each table which contains an IDENTITY column, execute the following SQL statement:
109- // DBCC CHECKIDENT ('[<schema>].[<table>]', RESEED, <seed>)
110- database . ExecuteNonQuery ( @"
111- DECLARE @sqlcmd VARCHAR(MAX);
112-
113- SET @sqlcmd = (
114- SELECT STRING_AGG(CAST('DBCC CHECKIDENT (''[' + [s].[name] + '].[' + [t].[name] + ']'', RESEED, ' + CAST([ic].[seed_value] AS VARCHAR(20)) + ')' AS NVARCHAR(MAX)),';' + CHAR(10)) WITHIN GROUP (ORDER BY [t].[name])
115- FROM
116- [sys].[schemas] AS [s],
117- [sys].[tables] AS [t],
118- [sys].[columns] AS [c],
119- [sys].[identity_columns] AS [ic]
120- WHERE
121- [s].[schema_id] = [t].[schema_id]
122- AND [t].[object_id] = [c].[object_id]
123- AND [c].[is_identity] = 1
124- AND [c].[object_id] = [ic].[object_id]
125- AND [c].[column_id] = [ic].[column_id]
126- )
127-
128- EXEC (@sqlcmd)" ) ;
103+ foreach ( var statement in GetClearDataStatements ( ) )
104+ {
105+ database . ExecuteNonQuery ( statement ) ;
106+ }
107+ }
129108
130- database . ExecuteNonQuery ( "EXEC sp_msforeachtable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all'" ) ;
109+ /// <summary>
110+ /// Clear all in the database asynchronously.
111+ /// </summary>
112+ /// <param name="database">SQL Server database which the data have to be deleted.</param>
113+ /// <param name="cancellationToken"><see cref="CancellationToken"/> used to cancel the asynchronous operation.</param>
114+ /// <returns>A <see cref="Task"/> which represents the asynchronous operation.</returns>
115+ public static async Task ClearAllDataAsync ( this SqlServerDatabase database , CancellationToken cancellationToken = default )
116+ {
117+ foreach ( var statement in GetClearDataStatements ( ) )
118+ {
119+ await database . ExecuteNonQueryAsync ( statement , cancellationToken ) ;
120+ }
131121 }
132122
133123 /// <summary>
@@ -243,6 +233,34 @@ Type t when Array.Exists(AuthorizedNonStringTypes, at => at == t) => builder.Add
243233 return statement ;
244234 }
245235
236+ private static string [ ] GetClearDataStatements ( )
237+ {
238+ return
239+ [
240+ "EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'" ,
241+ "EXEC sp_msforeachtable 'SET QUOTED_IDENTIFIER ON; DELETE FROM ?'" ,
242+ @"DECLARE @sqlcmd VARCHAR(MAX);
243+
244+ SET @sqlcmd = (
245+ SELECT STRING_AGG(CAST('DBCC CHECKIDENT (''[' + [s].[name] + '].[' + [t].[name] + ']'', RESEED, ' + CAST([ic].[seed_value] AS VARCHAR(20)) + ')' AS NVARCHAR(MAX)),';' + CHAR(10)) WITHIN GROUP (ORDER BY [t].[name])
246+ FROM
247+ [sys].[schemas] AS [s],
248+ [sys].[tables] AS [t],
249+ [sys].[columns] AS [c],
250+ [sys].[identity_columns] AS [ic]
251+ WHERE
252+ [s].[schema_id] = [t].[schema_id]
253+ AND [t].[object_id] = [c].[object_id]
254+ AND [c].[is_identity] = 1
255+ AND [c].[object_id] = [ic].[object_id]
256+ AND [c].[column_id] = [ic].[column_id]
257+ )
258+
259+ EXEC (@sqlcmd)" ,
260+ "EXEC sp_msforeachtable 'ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all'" ,
261+ ] ;
262+ }
263+
246264 private sealed class SqlInsertStatementBuilder
247265 {
248266 private readonly string tableName ;
0 commit comments