Table of Contents

Interface IDbTransactionInterceptor

Namespace
Microsoft.EntityFrameworkCore.Diagnostics
Assembly
Microsoft.EntityFrameworkCore.Relational.dll

Allows interception of operations related to a DbTransaction.

public interface IDbTransactionInterceptor : IInterceptor

Remarks

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.

See EF Core interceptors for more information and examples.

Methods

CreatedSavepoint(DbTransaction, TransactionEventData)

Called immediately after EF creates a transaction savepoint.

void CreatedSavepoint(DbTransaction transaction, TransactionEventData eventData)

Parameters

transaction DbTransaction

The transaction.

eventData TransactionEventData

Contextual 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 DbTransaction

The transaction.

eventData TransactionEventData

Contextual information about connection and transaction.

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

Task

A Task representing the asynchronous operation.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

CreatingSavepoint(DbTransaction, TransactionEventData, InterceptionResult)

Called just before EF intends to create a transaction savepoint.

InterceptionResult CreatingSavepoint(DbTransaction transaction, TransactionEventData eventData, InterceptionResult result)

Parameters

transaction DbTransaction

The transaction.

eventData TransactionEventData

Contextual information about connection and transaction.

result InterceptionResult

Represents 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, then EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. An 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 DbTransaction

The transaction.

eventData TransactionEventData

Contextual information about connection and transaction.

result InterceptionResult

Represents 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 CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

ValueTask<InterceptionResult>

If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, then EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. An implementation of this method for any interceptor that is not attempting to suppress the operation is to return the result value passed in.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

ReleasedSavepoint(DbTransaction, TransactionEventData)

Called immediately after EF releases a transaction savepoint.

void ReleasedSavepoint(DbTransaction transaction, TransactionEventData eventData)

Parameters

transaction DbTransaction

The transaction.

eventData TransactionEventData

Contextual 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 DbTransaction

The transaction.

eventData TransactionEventData

Contextual information about connection and transaction.

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

Task

A Task representing the asynchronous operation.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

ReleasingSavepoint(DbTransaction, TransactionEventData, InterceptionResult)

Called just before EF intends to release a transaction savepoint.

InterceptionResult ReleasingSavepoint(DbTransaction transaction, TransactionEventData eventData, InterceptionResult result)

Parameters

transaction DbTransaction

The transaction.

eventData TransactionEventData

Contextual information about connection and transaction.

result InterceptionResult

Represents 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, then EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. An 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 DbTransaction

The transaction.

eventData TransactionEventData

Contextual information about connection and transaction.

result InterceptionResult

Represents 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 CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

ValueTask<InterceptionResult>

If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, then EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. An implementation of this method for any interceptor that is not attempting to suppress the operation is to return the result value passed in.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

RolledBackToSavepoint(DbTransaction, TransactionEventData)

Called immediately after EF rolls back to a transaction savepoint.

void RolledBackToSavepoint(DbTransaction transaction, TransactionEventData eventData)

Parameters

transaction DbTransaction

The transaction.

eventData TransactionEventData

Contextual 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 DbTransaction

The transaction.

eventData TransactionEventData

Contextual information about connection and transaction.

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

Task

A Task representing the asynchronous operation.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

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 DbTransaction

The transaction.

eventData TransactionEventData

Contextual information about connection and transaction.

result InterceptionResult

Represents 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, then EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. An 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 DbTransaction

The transaction.

eventData TransactionEventData

Contextual information about connection and transaction.

result InterceptionResult

Represents 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 CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

ValueTask<InterceptionResult>

If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, then EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. An implementation of this method for any interceptor that is not attempting to suppress the operation is to return the result value passed in.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

TransactionCommitted(DbTransaction, TransactionEndEventData)

Called immediately after EF calls Commit().

void TransactionCommitted(DbTransaction transaction, TransactionEndEventData eventData)

Parameters

transaction DbTransaction

The transaction.

eventData TransactionEndEventData

Contextual 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 DbTransaction

The transaction.

eventData TransactionEndEventData

Contextual information about connection and transaction.

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

Task

A Task representing the asynchronous operation.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

TransactionCommitting(DbTransaction, TransactionEventData, InterceptionResult)

Called just before EF intends to call Commit().

InterceptionResult TransactionCommitting(DbTransaction transaction, TransactionEventData eventData, InterceptionResult result)

Parameters

transaction DbTransaction

The transaction.

eventData TransactionEventData

Contextual information about connection and transaction.

result InterceptionResult

Represents 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, then EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. An 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 DbTransaction

The transaction.

eventData TransactionEventData

Contextual information about connection and transaction.

result InterceptionResult

Represents 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 CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

ValueTask<InterceptionResult>

If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, then EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. An implementation of this method for any interceptor that is not attempting to suppress the operation is to return the result value passed in.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

TransactionFailed(DbTransaction, TransactionErrorEventData)

Called when use of a DbTransaction has failed with an exception.

void TransactionFailed(DbTransaction transaction, TransactionErrorEventData eventData)

Parameters

transaction DbTransaction

The transaction.

eventData TransactionErrorEventData

Contextual 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 DbTransaction

The transaction.

eventData TransactionErrorEventData

Contextual information about connection and transaction.

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

Task

A Task representing the asynchronous operation.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

TransactionRolledBack(DbTransaction, TransactionEndEventData)

Called immediately after EF calls Rollback().

void TransactionRolledBack(DbTransaction transaction, TransactionEndEventData eventData)

Parameters

transaction DbTransaction

The transaction.

eventData TransactionEndEventData

Contextual 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 DbTransaction

The transaction.

eventData TransactionEndEventData

Contextual information about connection and transaction.

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

Task

A Task representing the asynchronous operation.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

TransactionRollingBack(DbTransaction, TransactionEventData, InterceptionResult)

Called just before EF intends to call Rollback().

InterceptionResult TransactionRollingBack(DbTransaction transaction, TransactionEventData eventData, InterceptionResult result)

Parameters

transaction DbTransaction

The transaction.

eventData TransactionEventData

Contextual information about connection and transaction.

result InterceptionResult

Represents 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, then EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. An 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 DbTransaction

The transaction.

eventData TransactionEventData

Contextual information about connection and transaction.

result InterceptionResult

Represents 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 CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

ValueTask<InterceptionResult>

If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is false, then EF will continue as normal. If Microsoft.EntityFrameworkCore.Diagnostics.InterceptionResult.IsSuppressed is true, then EF will suppress the operation it was about to perform. An implementation of this method for any interceptor that is not attempting to suppress the operation is to return the result value passed in.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

TransactionStarted(DbConnection, TransactionEndEventData, DbTransaction)

Called immediately after EF calls BeginTransaction(IsolationLevel).

DbTransaction TransactionStarted(DbConnection connection, TransactionEndEventData eventData, DbTransaction result)

Parameters

connection DbConnection

The connection.

eventData TransactionEndEventData

Contextual information about connection and transaction.

result DbTransaction

The 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. An implementation of this method for any interceptor that is not attempting to change the result is to return the result value passed in.

Remarks

TransactionStartedAsync(DbConnection, TransactionEndEventData, DbTransaction, CancellationToken)

ValueTask<DbTransaction> TransactionStartedAsync(DbConnection connection, TransactionEndEventData eventData, DbTransaction result, CancellationToken cancellationToken = default)

Parameters

connection DbConnection

The connection.

eventData TransactionEndEventData

Contextual information about connection and transaction.

result DbTransaction

The result of the call to BeginTransactionAsync(IsolationLevel, CancellationToken). This value is typically used as the return value for the implementation of this method.

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

ValueTask<DbTransaction>

A Task providing the result that EF will use. An 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)

Remarks

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

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 DbConnection

The connection.

eventData TransactionStartingEventData

Contextual 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. An 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 DbConnection

The connection.

eventData TransactionStartingEventData

Contextual 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 CancellationToken

A CancellationToken to observe while waiting for the task to complete.

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. An 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)

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

TransactionUsed(DbConnection, TransactionEventData, DbTransaction)

Called immediately after is called.

DbTransaction TransactionUsed(DbConnection connection, TransactionEventData eventData, DbTransaction result)

Parameters

connection DbConnection

The connection.

eventData TransactionEventData

Contextual information about connection and transaction.

result DbTransaction

The DbTransaction that was passed to . 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 An 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 is called.

ValueTask<DbTransaction> TransactionUsedAsync(DbConnection connection, TransactionEventData eventData, DbTransaction result, CancellationToken cancellationToken = default)

Parameters

connection DbConnection

The connection.

eventData TransactionEventData

Contextual information about connection and transaction.

result DbTransaction

The DbTransaction that was passed to . This value is typically used as the return value for the implementation of this method.

cancellationToken CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

ValueTask<DbTransaction>

A Task containing the value that will be used as the effective value passed to An 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)

Exceptions

OperationCanceledException

If the CancellationToken is canceled.