Table of Contents

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

set DbSet<T>

The data set to remove entities from.

predicate Expression<Func<T, bool>>

The function for determining the items to remove.

Type Parameters

T

The 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

context DbContext

The DbContext to update entity audit info for.

userId string

The 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

context DbContext

The DbContext to update entity dates for.

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

context TContext

The DbContext.

action Func<TContext, Task>

The action to run.

onError Action<Exception>

An exception for handling the exception thrown, for example, event logging.

cancellationToken CancellationToken

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

Returns

Task<bool>

True if the action ran successfully; otherwise, false.

Type Parameters

TContext

The type of data context.

Exceptions

Exception

Potentially thrown by the onError delegate 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

context DbContext

The DbContext.

onError Action<Exception>

An exception for handling the exception thrown, for example, event logging.

cancellationToken CancellationToken

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

Returns

Task<bool>

True if the changes saved successfully; otherwise, false.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

Exception

Potentially thrown by the onError delegate 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

context DbContext

The DbContext.

entity T

The entity to update.

cancellationToken CancellationToken

The cancellation token.

Returns

Task

An asynchronous operation.

Type Parameters

T

The 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.