Table of Contents

Interface ISaveChangesInterceptor

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

Allows interception of the and methods.

public interface ISaveChangesInterceptor : IInterceptor

Remarks

SaveChanges interceptors can be used to view, change, or suppress execution of the SaveChanges call and modify the result before it is returned to EF.

Consider inheriting from SaveChangesInterceptor if not implementing all methods.

Use AddInterceptors(params 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

SaveChangesCanceled(DbContextEventData)

Called when was canceled.

void SaveChangesCanceled(DbContextEventData eventData)

Parameters

eventData DbContextEventData

Contextual information about the failure.

SaveChangesCanceledAsync(DbContextEventData, CancellationToken)

Called when was canceled.

Task SaveChangesCanceledAsync(DbContextEventData eventData, CancellationToken cancellationToken = default)

Parameters

eventData DbContextEventData

Contextual information about the failure.

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.

SaveChangesFailed(DbContextErrorEventData)

Called when an exception has been thrown in .

void SaveChangesFailed(DbContextErrorEventData eventData)

Parameters

eventData DbContextErrorEventData

Contextual information about the failure.

SaveChangesFailedAsync(DbContextErrorEventData, CancellationToken)

Called when an exception has been thrown in .

Task SaveChangesFailedAsync(DbContextErrorEventData eventData, CancellationToken cancellationToken = default)

Parameters

eventData DbContextErrorEventData

Contextual information about the failure.

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.

SavedChanges(SaveChangesCompletedEventData, int)

Called at the end of .

int SavedChanges(SaveChangesCompletedEventData eventData, int result)

Parameters

eventData SaveChangesCompletedEventData

Contextual information about the DbContext being used.

result int

The result of the call to . This value is typically used as the return value for the implementation of this method.

Returns

int

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

This method is still called if an interceptor suppressed creation of a command in SavingChanges(DbContextEventData, InterceptionResult<int>). In this case, result is the result returned by SavingChanges(DbContextEventData, InterceptionResult<int>).

SavedChangesAsync(SaveChangesCompletedEventData, int, CancellationToken)

Called at the end of .

ValueTask<int> SavedChangesAsync(SaveChangesCompletedEventData eventData, int result, CancellationToken cancellationToken = default)

Parameters

eventData SaveChangesCompletedEventData

Contextual information about the DbContext being used.

result int

The result of the call 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<int>

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

This method is still called if an interceptor suppressed creation of a command in SavingChangesAsync(DbContextEventData, InterceptionResult<int>, CancellationToken). In this case, result is the result returned by SavingChangesAsync(DbContextEventData, InterceptionResult<int>, CancellationToken).

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

SavingChanges(DbContextEventData, InterceptionResult<int>)

Called at the start of .

InterceptionResult<int> SavingChanges(DbContextEventData eventData, InterceptionResult<int> result)

Parameters

eventData DbContextEventData

Contextual information about the DbContext being used.

result InterceptionResult<int>

Represents the current result if one exists. This value will have HasResult set to true if some previous interceptor suppressed execution by calling SuppressWithResult(TResult). This value is typically used as the return value for the implementation of this method.

Returns

InterceptionResult<int>

If HasResult is false, the EF will continue as normal. If HasResult is true, then EF will suppress the operation it was about to perform and use 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.

SavingChangesAsync(DbContextEventData, InterceptionResult<int>, CancellationToken)

Called at the start of .

ValueTask<InterceptionResult<int>> SavingChangesAsync(DbContextEventData eventData, InterceptionResult<int> result, CancellationToken cancellationToken = default)

Parameters

eventData DbContextEventData

Contextual information about the DbContext being used.

result InterceptionResult<int>

Represents the current result if one exists. This value will have HasResult set to true if some previous interceptor suppressed execution by calling SuppressWithResult(TResult). 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<int>>

If HasResult is false, the EF will continue as normal. If HasResult is true, then EF will suppress the operation it was about to perform and use 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.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

ThrowingConcurrencyException(ConcurrencyExceptionEventData, InterceptionResult)

Called immediately before EF is going to throw a DbUpdateConcurrencyException.

InterceptionResult ThrowingConcurrencyException(ConcurrencyExceptionEventData eventData, InterceptionResult result)

Parameters

eventData ConcurrencyExceptionEventData

Contextual information about the concurrency conflict.

result InterceptionResult

Represents the current result if one exists. This value will have IsSuppressed set to true if some previous interceptor suppressed execution by calling Suppress(). This value is typically used as the return value for the implementation of this method.

Returns

InterceptionResult

If IsSuppressed is false, then EF will throw the exception. If IsSuppressed is true, then EF will not throw the exception. An implementation of this method for any interceptor that is not attempting to suppress setting property values must return the result value passed in.

ThrowingConcurrencyExceptionAsync(ConcurrencyExceptionEventData, InterceptionResult, CancellationToken)

Called immediately before EF is going to throw a DbUpdateConcurrencyException.

ValueTask<InterceptionResult> ThrowingConcurrencyExceptionAsync(ConcurrencyExceptionEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default)

Parameters

eventData ConcurrencyExceptionEventData

Contextual information about the concurrency conflict.

result InterceptionResult

Represents the current result if one exists. This value will have IsSuppressed set to true if some previous interceptor suppressed execution by calling 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 IsSuppressed is false, then EF will throw the exception. If IsSuppressed is true, then EF will not throw the exception. An implementation of this method for any interceptor that is not attempting to suppress setting property values must return the result value passed in.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.