Data/CafeLib.Data.Sources/SqlCommandProvider.cs
/// <summary>
/// Bulk Insert or update of entities.
/// </summary>
/// <typeparam name="TEntity">The type being inserted.</typeparam>
/// <param name="connectionInfo">Connection info</param>
/// <param name="data">Collection of entity records</param>
/// <param name="expressions"></param>
/// <param name="token">Cancellation token</param>
/// <returns></returns>
Bulk Insert or update of entities.
Bulk Insert or update of entities .
public async Task<int> UpsertAsync<TEntity>(IConnectionInfo connectionInfo, IEnumerable<TEntity> data, Expression<Func<TEntity, object>>[] expressions, CancellationToken token = default) where TEntity : class, IEntity
        {
            if (data == null) throw new ArgumentNullException(nameof(data));
            var results = 0;
            await using var connection = connectionInfo.GetConnection<T>();
            await connection.EnsureOpenAsync(cancellationToken: token).ConfigureAwait(false);
            using var transaction = connection.BeginTransaction();
            try
            {
                results = await connection.MergeAllAsync(data, transaction: transaction, cancellationToken: token).ConfigureAwait(false);
                transaction.Commit();
            }
            catch (Exception)
            {
                try
                {
                    transaction.Rollback();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return results;
        }
public async Task < int > UpsertAsync < TEntity > ( IConnectionInfo connectionInfo , IEnumerable < TEntity > data , Expression < Func < TEntity , object > > [ ] expressions , CancellationToken token = default ) where TEntity : class , IEntity { if ( data == null ) throw new ArgumentNullException ( nameof ( data ) ) ; var results = 0 ; await using var connection = connectionInfo . GetConnection < T > ( ) ; await connection . EnsureOpenAsync ( cancellationToken : token ) . ConfigureAwait ( false ) ; using var transaction = connection . BeginTransaction ( ) ; try { results = await connection . MergeAllAsync ( data , transaction : transaction , cancellationToken : token ) . ConfigureAwait ( false ) ; transaction . Commit ( ) ; } catch ( Exception ) { try { transaction . Rollback ( ) ; } catch ( Exception ex ) { throw ex ; } } return results ; }
Bulk Insert or update of entities.
Bulk Insert or update of entities .
// ReSharper disable once PossibleIntendedRethrow
None
connectionInfo
IConnectionInfo
None
data
IEnumerable<TEntity>
Collection of entity records
Collection of entity records
expressions
Expression<Func<TEntity, object>>[]
None
token
CancellationToken
None
typeparam
The type being inserted.
The type being inserted .