@@ -175,7 +175,11 @@ public Table(Database<TDatabase> database, string likelyTableName)
175
175
176
176
private DbConnection _connection ;
177
177
private int _commandTimeout ;
178
- private DbTransaction _transaction ;
178
+
179
+ /// <summary>
180
+ /// Get access to the underlying transaction
181
+ /// </summary>
182
+ public DbTransaction Transaction ;
179
183
180
184
/// <summary>
181
185
/// Get underlying database connection.
@@ -215,27 +219,29 @@ internal virtual Action<TDatabase> CreateTableConstructorForTable()
215
219
/// Begins a transaction in this database.
216
220
/// </summary>
217
221
/// <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 )
219
224
{
220
- _transaction = _connection . BeginTransaction ( isolation ) ;
225
+ Transaction = _connection . BeginTransaction ( isolation ) ;
226
+ return Transaction ;
221
227
}
222
228
223
229
/// <summary>
224
230
/// Commits the current transaction in this database.
225
231
/// </summary>
226
232
public void CommitTransaction ( )
227
233
{
228
- _transaction . Commit ( ) ;
229
- _transaction = null ;
234
+ Transaction . Commit ( ) ;
235
+ Transaction = null ;
230
236
}
231
237
232
238
/// <summary>
233
239
/// Rolls back the current transaction in this database.
234
240
/// </summary>
235
241
public void RollbackTransaction ( )
236
242
{
237
- _transaction . Rollback ( ) ;
238
- _transaction = null ;
243
+ Transaction . Rollback ( ) ;
244
+ Transaction = null ;
239
245
}
240
246
241
247
/// <summary>
@@ -336,7 +342,7 @@ private bool TableExists(string name)
336
342
if ( ! string . IsNullOrEmpty ( schemaName ) ) builder . Append ( "TABLE_SCHEMA = @schemaName AND " ) ;
337
343
builder . Append ( "TABLE_NAME = @name" ) ;
338
344
339
- return _connection . Query ( builder . ToString ( ) , new { schemaName , name } , _transaction ) . Count ( ) == 1 ;
345
+ return _connection . Query ( builder . ToString ( ) , new { schemaName , name } , Transaction ) . Count ( ) == 1 ;
340
346
}
341
347
342
348
/// <summary>
@@ -346,7 +352,7 @@ private bool TableExists(string name)
346
352
/// <param name="param">The parameters to use.</param>
347
353
/// <returns>The number of rows affected.</returns>
348
354
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 ) ;
350
356
351
357
/// <summary>
352
358
/// Queries the current database.
@@ -357,7 +363,7 @@ public int Execute(string sql, dynamic param = null) =>
357
363
/// <param name="buffered">Whether to buffer the results.</param>
358
364
/// <returns>An enumerable of <typeparamref name="T"/> for the rows fetched.</returns>
359
365
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 ) ;
361
367
362
368
/// <summary>
363
369
/// Queries the current database for a single record.
@@ -367,7 +373,7 @@ public IEnumerable<T> Query<T>(string sql, dynamic param = null, bool buffered =
367
373
/// <param name="param">The parameters to use.</param>
368
374
/// <returns>An enumerable of <typeparamref name="T"/> for the rows fetched.</returns>
369
375
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 ) ;
371
377
372
378
/// <summary>
373
379
/// Perform a multi-mapping query with 2 input types.
@@ -455,7 +461,7 @@ public IEnumerable<TReturn> Query<TFirst, TSecond, TThird, TFourth, TFifth, TRet
455
461
/// <param name="buffered">Whether the results should be buffered in memory.</param>
456
462
/// <remarks>Note: each row can be accessed via "dynamic", or by casting to an IDictionary<string,object></remarks>
457
463
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 ) ;
459
465
460
466
/// <summary>
461
467
/// Execute a command that returns multiple result sets, and access each in turn.
@@ -477,7 +483,7 @@ public virtual void Dispose()
477
483
if ( connection . State != ConnectionState . Closed )
478
484
{
479
485
_connection = null ;
480
- _transaction = null ;
486
+ Transaction = null ;
481
487
connection ? . Close ( ) ;
482
488
}
483
489
GC . SuppressFinalize ( this ) ;
0 commit comments