Class DbContextExtensions
- Namespace
- MADE.Data.EFCore.Extensions
- Assembly
- MADE.Data.EFCore.dll
Defines a collection of extensions for DbContext types.
public static class DbContextExtensions
- Inheritance
-
DbContextExtensions
- Inherited Members
Methods
RemoveWhere<T>(DbSet<T>, Expression<Func<T, bool>>)
Removes entities from a DbSet<TEntity> using the specified predicate.
public static void RemoveWhere<T>(this DbSet<T> set, Expression<Func<T, bool>> predicate) where T : class
Parameters
setDbSet<T>The data set to remove entities from.
predicateExpression<Func<T, bool>>The function for determining the items to remove.
Type Parameters
TThe type of entity to remove.
SetEntityAuditInfo(DbContext, string?)
Sets the audit information of IAuditableEntity entities being tracked in an added or modified state.
It is best to call this method in an override of the DbContext.SaveChangesAsync method in your data context.
public static void SetEntityAuditInfo(this DbContext context, string? userId)
Parameters
contextDbContextThe DbContext to update entity audit info for.
userIdstringThe identifier of the user performing the operation.
SetEntityDates(DbContext)
Sets the dates of EntityBase entities being tracked in an added or modified state.
It is best to call this method in an override of the DbContext.SaveChangesAsync method in your data context.
public static void SetEntityDates(this DbContext context)
Parameters
TryAsync<TContext>(TContext, Func<TContext, Task>?, Action<Exception>?, CancellationToken)
Attempts to perform an action on the data context.
public static Task<bool> TryAsync<TContext>(this TContext context, Func<TContext, Task>? action, Action<Exception>? onError = null, CancellationToken cancellationToken = default) where TContext : DbContext
Parameters
contextTContextThe DbContext.
actionFunc<TContext, Task>The action to run.
onErrorAction<Exception>An exception for handling the exception thrown, for example, event logging.
cancellationTokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
Type Parameters
TContextThe type of data context.
Exceptions
- Exception
Potentially thrown by the
onErrordelegate callback.- OperationCanceledException
If the CancellationToken is canceled.
TrySaveChangesAsync(DbContext, Action<Exception>?, CancellationToken)
Attempts to save all changes made in this context to the database.
public static Task<bool> TrySaveChangesAsync(this DbContext context, Action<Exception>? onError = null, CancellationToken cancellationToken = default)
Parameters
contextDbContextThe DbContext.
onErrorAction<Exception>An exception for handling the exception thrown, for example, event logging.
cancellationTokenCancellationTokenA CancellationToken to observe while waiting for the task to complete.
Returns
Exceptions
- OperationCanceledException
If the CancellationToken is canceled.
- Exception
Potentially thrown by the
onErrordelegate callback.
UpdateAsync<T>(DbContext, T, CancellationToken)
Updates an entity within the context and saves the changes.
public static Task UpdateAsync<T>(this DbContext context, T entity, CancellationToken cancellationToken = default)
Parameters
contextDbContextThe DbContext.
entityTThe entity to update.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task
An asynchronous operation.
Type Parameters
TThe type of entity to update.
Exceptions
- DbUpdateException
An error is encountered while saving to the database.
- DbUpdateConcurrencyException
A concurrency violation is encountered while saving to the database. A concurrency violation occurs when an unexpected number of rows are affected during save. This is usually because the data in the database has been modified since it was loaded into memory.
- OperationCanceledException
If the CancellationToken is canceled.