Class DbContextExtensions
Defines a collection of extensions for
Inheritance
Namespace: MADE.Data.EFCore.Extensions
Assembly: MADE.Data.EFCore.dll
Syntax
public static class DbContextExtensions : object
Methods
| Improve this Doc View SourceRemoveWhere<T>(DbSet<T>, Expression<Func<T, Boolean>>)
Removes entities from a
Declaration
public static void RemoveWhere<T>(this DbSet<T> set, Expression<Func<T, bool>> predicate)
where T : class
Parameters
Type | Name | Description |
---|---|---|
DbSet<T> | set | The data set to remove entities from. |
Expression<Func<T, System.Boolean>> | predicate | The function for determining the items to remove. |
Type Parameters
Name | Description |
---|---|
T | The type of entity to remove. |
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.
Declaration
public static void SetEntityDates(this DbContext context)
Parameters
Type | Name | Description |
---|---|---|
DbContext | context | The |
TryAsync<TContext>(TContext, Func<TContext, Task>, Action<Exception>)
Attempts to perform an action on the data context.
Declaration
public static async Task<bool> TryAsync<TContext>(this TContext context, Func<TContext, Task> action, Action<Exception> onError = null)
where TContext : DbContext
Parameters
Type | Name | Description |
---|---|---|
TContext | context | The |
Func<TContext, Task> | action | The action to run. |
Action<Exception> | onError | An exception for handling the exception thrown, for example, event logging. |
Returns
Type | Description |
---|---|
Task<System.Boolean> | True if the action ran successfully; otherwise, false. |
Type Parameters
Name | Description |
---|---|
TContext | The type of data context. |
TrySaveChangesAsync(DbContext, Action<Exception>, CancellationToken)
Attempts to save all changes made in this context to the database.
Declaration
public static async Task<bool> TrySaveChangesAsync(this DbContext context, Action<Exception> onError = null, CancellationToken cancellationToken = null)
Parameters
Type | Name | Description |
---|---|---|
DbContext | context | The |
Action<Exception> | onError | An exception for handling the exception thrown, for example, event logging. |
CancellationToken | cancellationToken | A |
Returns
Type | Description |
---|---|
Task<System.Boolean> | True if the changes saved successfully; otherwise, false. |
UpdateAsync<T>(DbContext, T, CancellationToken)
Updates an entity within the context and saves the changes.
Declaration
public static async Task UpdateAsync<T>(this DbContext context, T entity, CancellationToken cancellationToken = null)
Parameters
Type | Name | Description |
---|---|---|
DbContext | context | The |
T | entity | The entity to update. |
CancellationToken | cancellationToken | The cancellation token. |
Returns
Type | Description |
---|---|
Task | An asynchronous operation. |
Type Parameters
Name | Description |
---|---|
T | The type of entity to update. |