@@ -175,7 +175,11 @@ public Table(Database<TDatabase> database, string likelyTableName)
175175
176176 private DbConnection _connection ;
177177 private int _commandTimeout ;
178- private DbTransaction _transaction ;
178+
179+ /// <summary>
180+ /// Get access to the underlying transaction
181+ /// </summary>
182+ public DbTransaction Transaction ;
179183
180184 /// <summary>
181185 /// Get underlying database connection.
@@ -215,27 +219,29 @@ internal virtual Action<TDatabase> CreateTableConstructorForTable()
215219 /// Begins a transaction in this database.
216220 /// </summary>
217221 /// <param name="isolation">The isolation level to use.</param>
218- public void BeginTransaction ( IsolationLevel isolation = IsolationLevel . ReadCommitted )
222+ /// <returns>The transaction created</returns>
223+ public DbTransaction BeginTransaction ( IsolationLevel isolation = IsolationLevel . ReadCommitted )
219224 {
220- _transaction = _connection . BeginTransaction ( isolation ) ;
225+ Transaction = _connection . BeginTransaction ( isolation ) ;
226+ return Transaction ;
221227 }
222228
223229 /// <summary>
224230 /// Commits the current transaction in this database.
225231 /// </summary>
226232 public void CommitTransaction ( )
227233 {
228- _transaction . Commit ( ) ;
229- _transaction = null ;
234+ Transaction . Commit ( ) ;
235+ Transaction = null ;
230236 }
231237
232238 /// <summary>
233239 /// Rolls back the current transaction in this database.
234240 /// </summary>
235241 public void RollbackTransaction ( )
236242 {
237- _transaction . Rollback ( ) ;
238- _transaction = null ;
243+ Transaction . Rollback ( ) ;
244+ Transaction = null ;
239245 }
240246
241247 /// <summary>
@@ -336,7 +342,7 @@ private bool TableExists(string name)
336342 if ( ! string . IsNullOrEmpty ( schemaName ) ) builder . Append ( "TABLE_SCHEMA = @schemaName AND " ) ;
337343 builder . Append ( "TABLE_NAME = @name" ) ;
338344
339- return _connection . Query ( builder . ToString ( ) , new { schemaName , name } , _transaction ) . Count ( ) == 1 ;
345+ return _connection . Query ( builder . ToString ( ) , new { schemaName , name } , Transaction ) . Count ( ) == 1 ;
340346 }
341347
342348 /// <summary>
@@ -346,7 +352,7 @@ private bool TableExists(string name)
346352 /// <param name="param">The parameters to use.</param>
347353 /// <returns>The number of rows affected.</returns>
348354 public int Execute ( string sql , dynamic param = null ) =>
349- _connection . Execute ( sql , param as object , _transaction , _commandTimeout ) ;
355+ _connection . Execute ( sql , param as object , Transaction , _commandTimeout ) ;
350356
351357 /// <summary>
352358 /// Queries the current database.
@@ -357,7 +363,7 @@ public int Execute(string sql, dynamic param = null) =>
357363 /// <param name="buffered">Whether to buffer the results.</param>
358364 /// <returns>An enumerable of <typeparamref name="T"/> for the rows fetched.</returns>
359365 public IEnumerable < T > Query < T > ( string sql , dynamic param = null , bool buffered = true ) =>
360- _connection . Query < T > ( sql , param as object , _transaction , buffered , _commandTimeout ) ;
366+ _connection . Query < T > ( sql , param as object , Transaction , buffered , _commandTimeout ) ;
361367
362368 /// <summary>
363369 /// Queries the current database for a single record.
@@ -367,7 +373,7 @@ public IEnumerable<T> Query<T>(string sql, dynamic param = null, bool buffered =
367373 /// <param name="param">The parameters to use.</param>
368374 /// <returns>An enumerable of <typeparamref name="T"/> for the rows fetched.</returns>
369375 public T QueryFirstOrDefault < T > ( string sql , dynamic param = null ) =>
370- _connection . QueryFirstOrDefault < T > ( sql , param as object , _transaction , _commandTimeout ) ;
376+ _connection . QueryFirstOrDefault < T > ( sql , param as object , Transaction , _commandTimeout ) ;
371377
372378 /// <summary>
373379 /// Perform a multi-mapping query with 2 input types.
@@ -455,7 +461,7 @@ public IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TFifth, TRet
455461 /// <param name="buffered">Whether the results should be buffered in memory.</param>
456462 /// <remarks>Note: each row can be accessed via "dynamic", or by casting to an IDictionary<string,object></remarks>
457463 public IEnumerable < dynamic > Query ( string sql , dynamic param = null , bool buffered = true ) =>
458- _connection . Query ( sql , param as object , _transaction , buffered ) ;
464+ _connection . Query ( sql , param as object , Transaction , buffered ) ;
459465
460466 /// <summary>
461467 /// Execute a command that returns multiple result sets, and access each in turn.
@@ -477,7 +483,7 @@ public virtual void Dispose()
477483 if ( connection . State != ConnectionState . Closed )
478484 {
479485 _connection = null ;
480- _transaction = null ;
486+ Transaction = null ;
481487 connection ? . Close ( ) ;
482488 }
483489 GC . SuppressFinalize ( this ) ;
0 commit comments