Interface IDbContextCommands
- Namespace
- NuvTools.Data.EntityFrameworkCore.Context
- Assembly
- NuvTools.Data.EntityFrameworkCore.dll
Defines common database context operations for transaction management, CRUD operations with Result pattern, and execution strategies.
public interface IDbContextCommands
Properties
CurrentTransaction
Gets the current active transaction, or null if no transaction is active.
IDbContextTransaction? CurrentTransaction { get; }
Property Value
Methods
AcquireTransactionLockAsync(string, CancellationToken)
Acquires an exclusive, database-wide lock identified by name, held until the current
transaction commits or rolls back. Callers competing for the same name are serialized; different names
never block each other.
Task AcquireTransactionLockAsync(string name, CancellationToken cancellationToken = default)
Parameters
namestringLock name. Identifies what is being serialized, e.g.
"contract-number:53".cancellationTokenCancellationTokenA cancellation token to observe while waiting for the task to complete.
Returns
- Task
A task that completes once the lock is held.
Remarks
Use it to serialize a read-then-write that the schema alone cannot make atomic — allocating the next value of a per-tenant sequence, for instance, where every writer reads the same maximum and collides on a unique index. It is an advisory lock: it guards nothing by itself, only callers that ask for the same name.
A transaction must be active (see BeginTransactionAsync(CancellationToken)). Without one the lock is released as soon as the statement ends and protects nothing, so the call throws instead.
The statement itself lives in the provider package — pg_advisory_xact_lock in
NuvTools.Data.EntityFrameworkCore.PostgreSQL, sp_getapplock (LockOwner Transaction) in
NuvTools.Data.EntityFrameworkCore.SqlServer — behind IDbContextTransactionLock, and is
registered by that package's AddDatabase helpers. With no such package in play (an in-memory context
built by hand in a test) there is nothing to serialize and the call does nothing.
AddAndSaveAsync<TEntity, TKey>(TEntity, CancellationToken)
Adds a new entity to the database and saves changes, returning the entity's primary key.
Task<IResult<TKey>> AddAndSaveAsync<TEntity, TKey>(TEntity entity, CancellationToken cancellationToken = default) where TEntity : class
Parameters
entityTEntityThe entity to add.
cancellationTokenCancellationTokenA cancellation token to observe while waiting for the task to complete.
Returns
- Task<IResult<TKey>>
A task that represents the asynchronous operation, containing the result with the primary key.
Type Parameters
TEntityThe type of the entity.
TKeyThe type of the primary key.
AddAndSaveWithCompositeKeyAsync<TEntity>(TEntity, CancellationToken)
Adds a new entity with a composite key to the database and saves changes.
Task<IResult<object[]>> AddAndSaveWithCompositeKeyAsync<TEntity>(TEntity entity, CancellationToken cancellationToken = default) where TEntity : class
Parameters
entityTEntityThe entity to add.
cancellationTokenCancellationTokenA cancellation token to observe while waiting for the task to complete.
Returns
- Task<IResult<object[]>>
A task that represents the asynchronous operation, containing the result with the composite key values.
Type Parameters
TEntityThe type of the entity.
BeginTransactionAsync(CancellationToken)
Begins a new database transaction asynchronously.
Task<IDbContextTransaction> BeginTransactionAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenA cancellation token to observe while waiting for the task to complete.
Returns
- Task<IDbContextTransaction>
A task that represents the asynchronous operation, containing the database transaction.
CommitTransactionAsync(CancellationToken)
Commits the current transaction asynchronously.
Task CommitTransactionAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenA cancellation token to observe while waiting for the task to complete.
Returns
- Task
A task that represents the asynchronous operation.
ExecuteWithStrategyAsync(Func<CancellationToken, Task>, CancellationToken)
Executes an action within the database execution strategy for connection resiliency.
Task ExecuteWithStrategyAsync(Func<CancellationToken, Task> action, CancellationToken cancellationToken = default)
Parameters
actionFunc<CancellationToken, Task>The action to execute within the strategy.
cancellationTokenCancellationTokenA cancellation token to observe while waiting for the task to complete.
Returns
- Task
A task that represents the asynchronous operation.
- See Also
ExecuteWithStrategyAsync<T>(Func<CancellationToken, Task<T>>, CancellationToken)
Executes an action within the database execution strategy for connection resiliency and returns a result.
Task<T> ExecuteWithStrategyAsync<T>(Func<CancellationToken, Task<T>> action, CancellationToken cancellationToken = default)
Parameters
actionFunc<CancellationToken, Task<T>>The action to execute within the strategy.
cancellationTokenCancellationTokenA cancellation token to observe while waiting for the task to complete.
Returns
- Task<T>
A task that represents the asynchronous operation, containing the result of the action.
Type Parameters
TThe type of the result returned by the action.
- See Also
RemoveAndSaveAsync<TEntity>(params object[])
Removes an entity from the database and saves changes.
Task<IResult> RemoveAndSaveAsync<TEntity>(params object[] keyValues) where TEntity : class
Parameters
keyValuesobject[]The primary key values of the entity to remove.
Returns
- Task<IResult>
A task that represents the asynchronous operation, containing the result of the remove operation.
Type Parameters
TEntityThe type of the entity.
RemoveAndSaveAsync<TEntity>(object[], CancellationToken)
Removes an entity from the database and saves changes.
Task<IResult> RemoveAndSaveAsync<TEntity>(object[] keyValues, CancellationToken cancellationToken = default) where TEntity : class
Parameters
keyValuesobject[]The primary key values of the entity to remove.
cancellationTokenCancellationTokenA cancellation token to observe while waiting for the task to complete.
Returns
- Task<IResult>
A task that represents the asynchronous operation, containing the result of the remove operation.
Type Parameters
TEntityThe type of the entity.
RollbackTransactionAsync(CancellationToken)
Rolls back the current transaction asynchronously.
Task RollbackTransactionAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenA cancellation token to observe while waiting for the task to complete.
Returns
- Task
A task that represents the asynchronous operation.
SaveChangesAsync(CancellationToken)
Saves all changes made in this context to the database asynchronously.
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenA cancellation token to observe while waiting for the task to complete.
Returns
- Task<int>
A task that represents the asynchronous operation, containing the number of state entries written to the database.
UpdateAndSaveAsync<TEntity>(TEntity, params object[])
Updates an existing entity in the database and saves changes.
Task<IResult> UpdateAndSaveAsync<TEntity>(TEntity entity, params object[] keyValues) where TEntity : class
Parameters
entityTEntityThe entity with updated values.
keyValuesobject[]The primary key values of the entity to update.
Returns
- Task<IResult>
A task that represents the asynchronous operation, containing the result of the update operation.
Type Parameters
TEntityThe type of the entity.
UpdateAndSaveAsync<TEntity>(TEntity, object[], CancellationToken)
Updates an existing entity in the database and saves changes.
Task<IResult> UpdateAndSaveAsync<TEntity>(TEntity entity, object[] keyValues, CancellationToken cancellationToken = default) where TEntity : class
Parameters
entityTEntityThe entity with updated values.
keyValuesobject[]The primary key values of the entity to update.
cancellationTokenCancellationTokenA cancellation token to observe while waiting for the task to complete.
Returns
- Task<IResult>
A task that represents the asynchronous operation, containing the result of the update operation.
Type Parameters
TEntityThe type of the entity.