@@ -203,6 +203,18 @@ protected virtual IDbContextTransaction BeginTransaction(T ctx)
203203 return ctx . Database . BeginTransaction ( _sharedTablesIsolationLevel ) ;
204204 }
205205
206+ /// <summary>
207+ /// Starts a new transaction for migration and cleanup.
208+ /// </summary>
209+ /// <param name="ctx">Database context.</param>
210+ /// <returns>An instance of <see cref="IDbContextTransaction"/>.</returns>
211+ protected virtual IDbContextTransaction ? BeginMigrationAndCleanupTransaction ( T ctx )
212+ {
213+ ArgumentNullException . ThrowIfNull ( ctx ) ;
214+
215+ return ctx . Database . BeginTransaction ( _migrationAndCleanupIsolationLevel ) ;
216+ }
217+
206218 /// <summary>
207219 /// Runs migrations for provided <paramref name="ctx" />.
208220 /// </summary>
@@ -224,15 +236,16 @@ protected virtual void RunMigrations(T ctx)
224236 IDbContextTransaction ? migrationTx = null ;
225237
226238 if ( ctx . Database . CurrentTransaction is null )
227- migrationTx = ctx . Database . BeginTransaction ( _migrationAndCleanupIsolationLevel ) ;
239+ migrationTx = BeginMigrationAndCleanupTransaction ( ctx ) ;
228240
229241 try
230242 {
231243 _migrationExecutionStrategy . Migrate ( ctx ) ;
244+ migrationTx ? . Commit ( ) ;
232245 }
233246 finally
234247 {
235- migrationTx ? . Commit ( ) ;
248+ migrationTx ? . Dispose ( ) ;
236249 }
237250 }
238251 finally
@@ -294,15 +307,16 @@ private void DisposeContextsAndRollbackMigrations()
294307 IDbContextTransaction ? migrationTx = null ;
295308
296309 if ( ctx . Database . CurrentTransaction is null )
297- migrationTx = ctx . Database . BeginTransaction ( _migrationAndCleanupIsolationLevel ) ;
310+ migrationTx = BeginMigrationAndCleanupTransaction ( ctx ) ;
298311
299312 try
300313 {
301314 _isolationOptions . Cleanup ( ctx , Schema ) ;
315+ migrationTx ? . Commit ( ) ;
302316 }
303317 finally
304318 {
305- migrationTx ? . Commit ( ) ;
319+ migrationTx ? . Dispose ( ) ;
306320 }
307321 }
308322 }
0 commit comments