Interface IDbContextWithListCommands
- Namespace
- NuvTools.Data.EntityFrameworkCore.Context
- Assembly
- NuvTools.Data.EntityFrameworkCore.dll
Defines operations for synchronizing database entities with in-memory lists. Provides methods for bulk add, update, and remove operations.
public interface IDbContextWithListCommands
Methods
AddOrRemoveFromListAsync<TEntity, TKey>(IEnumerable<TEntity>, Func<TEntity, TKey>, Expression<Func<TEntity, bool>>?, CancellationToken)
Synchronizes a DbSet with a provided list by adding or removing entities, without updating any entities.
Task<IResult> AddOrRemoveFromListAsync<TEntity, TKey>(IEnumerable<TEntity> entities, Func<TEntity, TKey> keySelector, Expression<Func<TEntity, bool>>? filter = null, CancellationToken cancellationToken = default) where TEntity : class where TKey : notnull
Parameters
entitiesIEnumerable<TEntity>The list of entities to synchronize with the database.
keySelectorFunc<TEntity, TKey>A function to select the key for matching entities.
filterExpression<Func<TEntity, bool>>An optional filter predicate to apply to the database entities.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
Type Parameters
TEntityThe type of the entity.
TKeyThe type of the key used to uniquely identify entities.
Examples
await context.AddOrRemoveFromListAsync( updatedOrders, o => o.OrderId, o => o.IsPending );
Remarks
This method adds entities in the list that are not in the database and removes entities in the database that are not in the list. It does not update any entities.
AddOrUpdateFromListAsync<TEntity, TKey>(IEnumerable<TEntity>, Func<TEntity, TKey>, Expression<Func<TEntity, bool>>?, CancellationToken)
Synchronizes a DbSet with a provided list by adding or updating entities, without removing any entities.
Task<IResult> AddOrUpdateFromListAsync<TEntity, TKey>(IEnumerable<TEntity> entities, Func<TEntity, TKey> keySelector, Expression<Func<TEntity, bool>>? filter = null, CancellationToken cancellationToken = default) where TEntity : class where TKey : notnull
Parameters
entitiesIEnumerable<TEntity>The list of entities to synchronize with the database.
keySelectorFunc<TEntity, TKey>A function to select the key for matching entities.
filterExpression<Func<TEntity, bool>>An optional filter predicate to apply to the database entities.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
Type Parameters
TEntityThe type of the entity.
TKeyThe type of the key used to uniquely identify entities.
Examples
await context.AddOrUpdateFromListAsync( updatedProducts, p => p.ProductCode, p => p.IsActive );
Remarks
This method adds entities in the list that are not in the database and updates entities that exist in both. It does not remove any entities from the database.
SyncFromListAsync<TEntity, TKey>(IEnumerable<TEntity>, Func<TEntity, TKey>, Expression<Func<TEntity, bool>>?, CancellationToken)
Synchronizes a DbSet with a provided list by adding, updating, or removing entities.
Task<IResult> SyncFromListAsync<TEntity, TKey>(IEnumerable<TEntity> entities, Func<TEntity, TKey> keySelector, Expression<Func<TEntity, bool>>? filter = null, CancellationToken cancellationToken = default) where TEntity : class where TKey : notnull
Parameters
entitiesIEnumerable<TEntity>The list of entities to synchronize with the database.
keySelectorFunc<TEntity, TKey>A function to select the key for matching entities.
filterExpression<Func<TEntity, bool>>An optional filter predicate to apply to the database entities.
cancellationTokenCancellationTokenA token to monitor for cancellation requests.
Returns
Type Parameters
TEntityThe type of the entity.
TKeyThe type of the key used to uniquely identify entities.
Examples
await context.SyncFromListAsync( updatedEmployees, e => e.Id, e => e.IsActive );
Remarks
This method adds entities in the list that are not in the database, updates entities that exist in both, and removes entities in the database that are not in the list.