Interface IDbTransactionInterceptor
- Namespace
- Microsoft.EntityFrameworkCore.Diagnostics
- Assembly
- Microsoft.EntityFrameworkCore.Relational.dll
Allows interception of operations related to a DbTransaction.
Transaction interceptors can be used to view, change, or suppress operations on DbTransaction, and to modify the result before it is returned to EF.
Consider inheriting from DbTransactionInterceptor if not implementing all methods.
Use Microsoft.EntityFrameworkCore.DbContextOptionsBuilder.AddInterceptors(Microsoft.EntityFrameworkCore.Diagnostics.IInterceptor[]) to register application interceptors.
Extensions can also register interceptors in the internal service provider. If both injected and application interceptors are found, then the injected interceptors are run in the order that they are resolved from the service provider, and then the application interceptors are run last.
public interface IDbTransactionInterceptor : IInterceptor
Methods
CreatedSavepoint(DbTransaction, TransactionEventData)
Called immediately after EF creates a transaction savepoint.
void CreatedSavepoint(DbTransaction transaction, TransactionEventData eventData)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEventDataContextual information about connection and transaction.
CreatedSavepointAsync(DbTransaction, TransactionEventData, CancellationToken)
Called immediately after EF calls CommitAsync(CancellationToken).
Task CreatedSavepointAsync(DbTransaction transaction, TransactionEventData eventData, CancellationToken cancellationToken = default)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEventDataContextual information about connection and transaction.
cancellationToken
CancellationTokenThe cancellation token.
Returns
CreatingSavepoint(DbTransaction, TransactionEventData, InterceptionResult)
Called just before EF intends to create a transaction savepoint.
InterceptionResult CreatingSavepoint(DbTransaction transaction, TransactionEventData eventData, InterceptionResult result)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEventDataContextual information about connection and transaction.
result
InterceptionResultRepresents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.Suppress. This value is typically used as the return value for the implementation of this method.
Returns
- InterceptionResult
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, the EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. A normal implementation of this method for any interceptor that is not attempting to suppress the operation is to return the
result
value passed in.
CreatingSavepointAsync(DbTransaction, TransactionEventData, InterceptionResult, CancellationToken)
Called just before EF intends to create a transaction savepoint.
ValueTask<InterceptionResult> CreatingSavepointAsync(DbTransaction transaction, TransactionEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEventDataContextual information about connection and transaction.
result
InterceptionResultRepresents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.Suppress. This value is typically used as the return value for the implementation of this method.
cancellationToken
CancellationTokenThe cancellation token.
Returns
- ValueTask<InterceptionResult>
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, the EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. A normal implementation of this method for any interceptor that is not attempting to suppress the operation is to return the
result
value passed in.
ReleasedSavepoint(DbTransaction, TransactionEventData)
Called immediately after EF releases a transaction savepoint.
void ReleasedSavepoint(DbTransaction transaction, TransactionEventData eventData)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEventDataContextual information about connection and transaction.
ReleasedSavepointAsync(DbTransaction, TransactionEventData, CancellationToken)
Called immediately after EF releases a transaction savepoint.
Task ReleasedSavepointAsync(DbTransaction transaction, TransactionEventData eventData, CancellationToken cancellationToken = default)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEventDataContextual information about connection and transaction.
cancellationToken
CancellationTokenThe cancellation token.
Returns
ReleasingSavepoint(DbTransaction, TransactionEventData, InterceptionResult)
Called just before EF intends to release a transaction savepoint.
InterceptionResult ReleasingSavepoint(DbTransaction transaction, TransactionEventData eventData, InterceptionResult result)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEventDataContextual information about connection and transaction.
result
InterceptionResultRepresents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.Suppress. This value is typically used as the return value for the implementation of this method.
Returns
- InterceptionResult
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, the EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. A normal implementation of this method for any interceptor that is not attempting to suppress the operation is to return the
result
value passed in.
ReleasingSavepointAsync(DbTransaction, TransactionEventData, InterceptionResult, CancellationToken)
Called just before EF intends to release a transaction savepoint.
ValueTask<InterceptionResult> ReleasingSavepointAsync(DbTransaction transaction, TransactionEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEventDataContextual information about connection and transaction.
result
InterceptionResultRepresents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.Suppress. This value is typically used as the return value for the implementation of this method.
cancellationToken
CancellationTokenThe cancellation token.
Returns
- ValueTask<InterceptionResult>
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, the EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. A normal implementation of this method for any interceptor that is not attempting to suppress the operation is to return the
result
value passed in.
RolledBackToSavepoint(DbTransaction, TransactionEventData)
Called immediately after EF rolls back to a transaction savepoint.
void RolledBackToSavepoint(DbTransaction transaction, TransactionEventData eventData)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEventDataContextual information about connection and transaction.
RolledBackToSavepointAsync(DbTransaction, TransactionEventData, CancellationToken)
Called immediately after EF rolls back to a transaction savepoint.
Task RolledBackToSavepointAsync(DbTransaction transaction, TransactionEventData eventData, CancellationToken cancellationToken = default)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEventDataContextual information about connection and transaction.
cancellationToken
CancellationTokenThe cancellation token.
Returns
RollingBackToSavepoint(DbTransaction, TransactionEventData, InterceptionResult)
Called just before EF intends to roll back to a transaction savepoint.
InterceptionResult RollingBackToSavepoint(DbTransaction transaction, TransactionEventData eventData, InterceptionResult result)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEventDataContextual information about connection and transaction.
result
InterceptionResultRepresents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.Suppress. This value is typically used as the return value for the implementation of this method.
Returns
- InterceptionResult
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, the EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. A normal implementation of this method for any interceptor that is not attempting to suppress the operation is to return the
result
value passed in.
RollingBackToSavepointAsync(DbTransaction, TransactionEventData, InterceptionResult, CancellationToken)
Called just before EF intends to roll back to a transaction savepoint.
ValueTask<InterceptionResult> RollingBackToSavepointAsync(DbTransaction transaction, TransactionEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEventDataContextual information about connection and transaction.
result
InterceptionResultRepresents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.Suppress. This value is typically used as the return value for the implementation of this method.
cancellationToken
CancellationTokenThe cancellation token.
Returns
- ValueTask<InterceptionResult>
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, the EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. A normal implementation of this method for any interceptor that is not attempting to suppress the operation is to return the
result
value passed in.
TransactionCommitted(DbTransaction, TransactionEndEventData)
Called immediately after EF calls Commit().
void TransactionCommitted(DbTransaction transaction, TransactionEndEventData eventData)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEndEventDataContextual information about connection and transaction.
TransactionCommittedAsync(DbTransaction, TransactionEndEventData, CancellationToken)
Called immediately after EF calls CommitAsync(CancellationToken).
Task TransactionCommittedAsync(DbTransaction transaction, TransactionEndEventData eventData, CancellationToken cancellationToken = default)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEndEventDataContextual information about connection and transaction.
cancellationToken
CancellationTokenThe cancellation token.
Returns
TransactionCommitting(DbTransaction, TransactionEventData, InterceptionResult)
Called just before EF intends to call Commit().
InterceptionResult TransactionCommitting(DbTransaction transaction, TransactionEventData eventData, InterceptionResult result)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEventDataContextual information about connection and transaction.
result
InterceptionResultRepresents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.Suppress. This value is typically used as the return value for the implementation of this method.
Returns
- InterceptionResult
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, the EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. A normal implementation of this method for any interceptor that is not attempting to suppress the operation is to return the
result
value passed in.
TransactionCommittingAsync(DbTransaction, TransactionEventData, InterceptionResult, CancellationToken)
Called just before EF intends to call CommitAsync(CancellationToken).
ValueTask<InterceptionResult> TransactionCommittingAsync(DbTransaction transaction, TransactionEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEventDataContextual information about connection and transaction.
result
InterceptionResultRepresents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.Suppress. This value is typically used as the return value for the implementation of this method.
cancellationToken
CancellationTokenThe cancellation token.
Returns
- ValueTask<InterceptionResult>
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, the EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. A normal implementation of this method for any interceptor that is not attempting to suppress the operation is to return the
result
value passed in.
TransactionFailed(DbTransaction, TransactionErrorEventData)
Called when use of a DbTransaction has failed with an exception.
void TransactionFailed(DbTransaction transaction, TransactionErrorEventData eventData)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionErrorEventDataContextual information about connection and transaction.
TransactionFailedAsync(DbTransaction, TransactionErrorEventData, CancellationToken)
Called when use of a DbTransaction has failed with an exception.
Task TransactionFailedAsync(DbTransaction transaction, TransactionErrorEventData eventData, CancellationToken cancellationToken = default)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionErrorEventDataContextual information about connection and transaction.
cancellationToken
CancellationTokenThe cancellation token.
Returns
TransactionRolledBack(DbTransaction, TransactionEndEventData)
Called immediately after EF calls Rollback().
void TransactionRolledBack(DbTransaction transaction, TransactionEndEventData eventData)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEndEventDataContextual information about connection and transaction.
TransactionRolledBackAsync(DbTransaction, TransactionEndEventData, CancellationToken)
Called immediately after EF calls RollbackAsync(CancellationToken).
Task TransactionRolledBackAsync(DbTransaction transaction, TransactionEndEventData eventData, CancellationToken cancellationToken = default)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEndEventDataContextual information about connection and transaction.
cancellationToken
CancellationTokenThe cancellation token.
Returns
TransactionRollingBack(DbTransaction, TransactionEventData, InterceptionResult)
Called just before EF intends to call Rollback().
InterceptionResult TransactionRollingBack(DbTransaction transaction, TransactionEventData eventData, InterceptionResult result)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEventDataContextual information about connection and transaction.
result
InterceptionResultRepresents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.Suppress. This value is typically used as the return value for the implementation of this method.
Returns
- InterceptionResult
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, the EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. A normal implementation of this method for any interceptor that is not attempting to suppress the operation is to return the
result
value passed in.
TransactionRollingBackAsync(DbTransaction, TransactionEventData, InterceptionResult, CancellationToken)
Called just before EF intends to call RollbackAsync(CancellationToken).
ValueTask<InterceptionResult> TransactionRollingBackAsync(DbTransaction transaction, TransactionEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default)
Parameters
transaction
DbTransactionThe transaction.
eventData
TransactionEventDataContextual information about connection and transaction.
result
InterceptionResultRepresents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.Suppress. This value is typically used as the return value for the implementation of this method.
cancellationToken
CancellationTokenThe cancellation token.
Returns
- ValueTask<InterceptionResult>
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, the EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. A normal implementation of this method for any interceptor that is not attempting to suppress the operation is to return the
result
value passed in.
TransactionStarted(DbConnection, TransactionEndEventData, DbTransaction)
Called immediately after EF calls BeginTransaction(IsolationLevel).
This method is still called if an interceptor suppressed creation in TransactionStarting(DbConnection, TransactionStartingEventData, InterceptionResult<DbTransaction>).
In this case, result
is the result returned by TransactionStarting(DbConnection, TransactionStartingEventData, InterceptionResult<DbTransaction>).
DbTransaction TransactionStarted(DbConnection connection, TransactionEndEventData eventData, DbTransaction result)
Parameters
connection
DbConnectionThe connection.
eventData
TransactionEndEventDataContextual information about connection and transaction.
result
DbTransactionThe result of the call to BeginTransaction(IsolationLevel). This value is typically used as the return value for the implementation of this method.
Returns
- DbTransaction
The result that EF will use. A normal implementation of this method for any interceptor that is not attempting to change the result is to return the
result
value passed in.
TransactionStartedAsync(DbConnection, TransactionEndEventData, DbTransaction, CancellationToken)
Called immediately after EF calls BeginTransactionAsync(IsolationLevel, CancellationToken) .
This method is still called if an interceptor suppressed creation in TransactionStarting(DbConnection, TransactionStartingEventData, InterceptionResult<DbTransaction>).
In this case, result
is the result returned by TransactionStarting(DbConnection, TransactionStartingEventData, InterceptionResult<DbTransaction>).
ValueTask<DbTransaction> TransactionStartedAsync(DbConnection connection, TransactionEndEventData eventData, DbTransaction result, CancellationToken cancellationToken = default)
Parameters
connection
DbConnectionThe connection.
eventData
TransactionEndEventDataContextual information about connection and transaction.
result
DbTransactionThe result of the call to BeginTransactionAsync(IsolationLevel, CancellationToken). This value is typically used as the return value for the implementation of this method.
cancellationToken
CancellationTokenThe cancellation token.
Returns
- ValueTask<DbTransaction>
A Task providing the result that EF will use. A normal implementation of this method for any interceptor that is not attempting to change the result is to return the
result
value passed in, often using FromResult<TResult>(TResult)
TransactionStarting(DbConnection, TransactionStartingEventData, InterceptionResult<DbTransaction>)
Called just before EF intends to call BeginTransaction(IsolationLevel).
InterceptionResult<DbTransaction> TransactionStarting(DbConnection connection, TransactionStartingEventData eventData, InterceptionResult<DbTransaction> result)
Parameters
connection
DbConnectionThe connection.
eventData
TransactionStartingEventDataContextual information about connection and transaction.
result
InterceptionResult<DbTransaction>Represents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult`1.HasResult set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult`1.SuppressWithResult(`0). This value is typically used as the return value for the implementation of this method.
Returns
- InterceptionResult<DbTransaction>
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult`1.HasResult is false, the EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult`1.HasResult is true, then EF will suppress the operation it was about to perform and use Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult`1.Result instead. A normal implementation of this method for any interceptor that is not attempting to change the result is to return the
result
value passed in, often using FromResult<TResult>(TResult)
TransactionStartingAsync(DbConnection, TransactionStartingEventData, InterceptionResult<DbTransaction>, CancellationToken)
Called just before EF intends to call BeginTransactionAsync(IsolationLevel, CancellationToken).
ValueTask<InterceptionResult<DbTransaction>> TransactionStartingAsync(DbConnection connection, TransactionStartingEventData eventData, InterceptionResult<DbTransaction> result, CancellationToken cancellationToken = default)
Parameters
connection
DbConnectionThe connection.
eventData
TransactionStartingEventDataContextual information about connection and transaction.
result
InterceptionResult<DbTransaction>Represents the current result if one exists. This value will have Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult`1.HasResult set to true if some previous interceptor suppressed execution by calling Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult`1.SuppressWithResult(`0). This value is typically used as the return value for the implementation of this method.
cancellationToken
CancellationTokenThe cancellation token.
Returns
- ValueTask<InterceptionResult<DbTransaction>>
If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult`1.HasResult is false, the EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult`1.HasResult is true, then EF will suppress the operation it was about to perform and use Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult`1.Result instead. A normal implementation of this method for any interceptor that is not attempting to change the result is to return the
result
value passed in, often using FromResult<TResult>(TResult)
TransactionUsed(DbConnection, TransactionEventData, DbTransaction)
Called immediately after RelationalDatabaseFacadeExtensions.UseTransaction is called.
DbTransaction TransactionUsed(DbConnection connection, TransactionEventData eventData, DbTransaction result)
Parameters
connection
DbConnectionThe connection.
eventData
TransactionEventDataContextual information about connection and transaction.
result
DbTransactionThe DbTransaction that was passed to RelationalDatabaseFacadeExtensions.UseTransaction. This value is typically used as the return value for the implementation of this method.
Returns
- DbTransaction
The value that will be used as the effective value passed to RelationalDatabaseFacadeExtensions.UseTransaction A normal implementation of this method for any interceptor that is not attempting to change the result is to return the
result
value passed in.
TransactionUsedAsync(DbConnection, TransactionEventData, DbTransaction, CancellationToken)
Called immediately after RelationalDatabaseFacadeExtensions.UseTransactionAsync is called.
ValueTask<DbTransaction> TransactionUsedAsync(DbConnection connection, TransactionEventData eventData, DbTransaction result, CancellationToken cancellationToken = default)
Parameters
connection
DbConnectionThe connection.
eventData
TransactionEventDataContextual information about connection and transaction.
result
DbTransactionThe DbTransaction that was passed to RelationalDatabaseFacadeExtensions.UseTransactionAsync. This value is typically used as the return value for the implementation of this method.
cancellationToken
CancellationTokenThe cancellation token.
Returns
- ValueTask<DbTransaction>
A Task containing the value that will be used as the effective value passed to RelationalDatabaseFacadeExtensions.UseTransactionAsync A normal implementation of this method for any interceptor that is not attempting to change the result is to return the
result
value passed in, often using FromResult<TResult>(TResult)