Table of Contents

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

entities IEnumerable<TEntity>

The list of entities to synchronize with the database.

keySelector Func<TEntity, TKey>

A function to select the key for matching entities.

filter Expression<Func<TEntity, bool>>

An optional filter predicate to apply to the database entities.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<IResult>

Type Parameters

TEntity

The type of the entity.

TKey

The 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

entities IEnumerable<TEntity>

The list of entities to synchronize with the database.

keySelector Func<TEntity, TKey>

A function to select the key for matching entities.

filter Expression<Func<TEntity, bool>>

An optional filter predicate to apply to the database entities.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<IResult>

Type Parameters

TEntity

The type of the entity.

TKey

The 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

entities IEnumerable<TEntity>

The list of entities to synchronize with the database.

keySelector Func<TEntity, TKey>

A function to select the key for matching entities.

filter Expression<Func<TEntity, bool>>

An optional filter predicate to apply to the database entities.

cancellationToken CancellationToken

A token to monitor for cancellation requests.

Returns

Task<IResult>

Type Parameters

TEntity

The type of the entity.

TKey

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