Interface ISaveChangesInterceptor
- Namespace
- Microsoft.EntityFrameworkCore.Diagnostics
- Assembly
- Microsoft.EntityFrameworkCore.dll
Allows interception of the
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
void SaveChangesCanceled(DbContextEventData eventData)
Parameters
eventData
DbContextEventDataContextual information about the failure.
SaveChangesCanceledAsync(DbContextEventData, CancellationToken)
Called when
Task SaveChangesCanceledAsync(DbContextEventData eventData, CancellationToken cancellationToken = default)
Parameters
eventData
DbContextEventDataContextual information about the failure.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
Exceptions
- OperationCanceledException
If the CancellationToken is canceled.
SaveChangesFailed(DbContextErrorEventData)
Called when an exception has been thrown in
void SaveChangesFailed(DbContextErrorEventData eventData)
Parameters
eventData
DbContextErrorEventDataContextual information about the failure.
SaveChangesFailedAsync(DbContextErrorEventData, CancellationToken)
Called when an exception has been thrown in
Task SaveChangesFailedAsync(DbContextErrorEventData eventData, CancellationToken cancellationToken = default)
Parameters
eventData
DbContextErrorEventDataContextual information about the failure.
cancellationToken
CancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
Exceptions
- OperationCanceledException
If the CancellationToken is canceled.
SavedChanges(SaveChangesCompletedEventData, int)
Called at the end of
int SavedChanges(SaveChangesCompletedEventData eventData, int result)
Parameters
eventData
SaveChangesCompletedEventDataContextual information about the DbContext being used.
result
intThe 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
SaveChangesCompletedEventDataContextual information about the DbContext being used.
result
intThe result of the call to
. This value is typically used as the return value for the implementation of this method. cancellationToken
CancellationTokenA 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
DbContextEventDataContextual 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
DbContextEventDataContextual 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
CancellationTokenA 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
ConcurrencyExceptionEventDataContextual information about the concurrency conflict.
result
InterceptionResultRepresents 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
ConcurrencyExceptionEventDataContextual information about the concurrency conflict.
result
InterceptionResultRepresents 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
CancellationTokenA 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.