Class DbContextExtensions
- Namespace
- Trellis.EntityFrameworkCore
- Assembly
- Trellis.EntityFrameworkCore.dll
Extension methods on DbContext that wrap SaveChangesAsync(CancellationToken) and convert expected database exceptions to Result<TValue> failures.
public static class DbContextExtensions
- Inheritance
-
DbContextExtensions
- Inherited Members
Methods
SaveChangesResultAsync(DbContext, bool, CancellationToken)
Calls SaveChangesAsync(bool, CancellationToken) and converts expected database exceptions
to Result<TValue> failures. The acceptAllChangesOnSuccess parameter controls whether
AcceptAllChanges()
is called after saving successfully.
Expected exceptions converted:
- DbUpdateConcurrencyException → Error.Conflict with detail
- DbUpdateException (duplicate key) → Error.Conflict with detail
- DbUpdateException (foreign key violation) → Error.Conflict with detail
Unexpected exceptions (connection failures, timeouts, etc.) are NOT caught. They propagate as exceptions for global exception handlers and retry policies.
OperationCanceledException is NOT caught (re-throws).
public static Task<Result<int>> SaveChangesResultAsync(this DbContext context, bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
Parameters
contextDbContextThe DbContext to save changes on.
acceptAllChangesOnSuccessbooltrue to accept all changes after saving (default EF Core behavior); false to leave the change tracker state unchanged.
cancellationTokenCancellationTokenA token to observe while waiting for the task to complete.
Returns
- Task<Result<int>>
A Result<TValue> containing the number of state entries written on success, or an error on failure.
SaveChangesResultAsync(DbContext, CancellationToken)
Convenience overload: delegates to
SaveChangesResultAsync(DbContext, bool, CancellationToken) with
acceptAllChangesOnSuccess set to true (EF Core's default behavior).
public static Task<Result<int>> SaveChangesResultAsync(this DbContext context, CancellationToken cancellationToken = default)
Parameters
contextDbContextThe DbContext to save changes on.
cancellationTokenCancellationTokenA token to observe while waiting for the task to complete.
Returns
- Task<Result<int>>
A Result<TValue> containing the number of state entries written on success, or an error on failure.
SaveChangesResultUnitAsync(DbContext, bool, CancellationToken)
Convenience overload: calls SaveChangesResultAsync(DbContext, bool, CancellationToken) and maps success to Result<TValue> with Unit. Use when callers don't need the affected row count.
public static Task<Result<Unit>> SaveChangesResultUnitAsync(this DbContext context, bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
Parameters
contextDbContextThe DbContext to save changes on.
acceptAllChangesOnSuccessbooltrue to accept all changes after saving (default EF Core behavior); false to leave the change tracker state unchanged.
cancellationTokenCancellationTokenA token to observe while waiting for the task to complete.
Returns
- Task<Result<Unit>>
A Result<TValue> with Unit representing success or failure.
SaveChangesResultUnitAsync(DbContext, CancellationToken)
Convenience overload: calls SaveChangesResultAsync(DbContext, CancellationToken) and maps success to Result<TValue> with Unit. Use when callers don't need the affected row count.
public static Task<Result<Unit>> SaveChangesResultUnitAsync(this DbContext context, CancellationToken cancellationToken = default)
Parameters
contextDbContextThe DbContext to save changes on.
cancellationTokenCancellationTokenA token to observe while waiting for the task to complete.
Returns
- Task<Result<Unit>>
A Result<TValue> with Unit representing success or failure.