private static async Task InsertOrUpdate <T>(IDbConnection connection, List <T> records, string tableName) where T : class { if (!records.Any()) { return; } var data = connection.Query <T>($"SELECT * FROM {tableName}").ToList(); var updates = records.Where(x => data.Contains(x)).ToList(); var inserts = records.Where(x => !data.Contains(x)).ToList(); if (inserts.Any()) { await connection .BulkActionAsync(x => x.BulkInsert(inserts)); } if (updates.Any()) { await connection .BulkActionAsync(x => x.BulkUpdate(updates)); } }
private static Task Insert <T>(IDbConnection connection, List <T> records) where T : class { if (records.Any()) { return(connection .BulkActionAsync(x => x.BulkInsert(records))); } return(Task.CompletedTask); }
private async Task DeleteReferredTables(IDbConnection connection, ClientDto data) { var branches = _mapper.Map <IEnumerable <BranchesDto>, IEnumerable <cliDelega> >(data.BranchesDto); var contacts = _mapper.Map <IEnumerable <ContactsDto>, IEnumerable <CliContactos> >(data.ContactsDto); var visitas = _mapper.Map <IEnumerable <VisitsDto>, IEnumerable <Visitas> >(data.VisitsDto); await connection.BulkActionAsync(x => { if (branches.Count() > 0) { x.BulkDelete(branches); } if (contacts.Count() > 0) { x.BulkDelete(contacts); } if (visitas.Count() > 0) { x.BulkDelete(visitas); } }); }
/// <summary> /// Delete offices asynchronously. /// </summary> /// <param name="dto">Fata transfer object to delete the offices.</param> /// <returns>True if the action has been completed successfully</returns> private async Task <bool> DeleteOfficesAsync(IDbConnection connection, IEnumerable <OfficeDtos> listOfOffices) { IEnumerable <OFICINAS> offices = _mapper.Map <IEnumerable <OfficeDtos>, IEnumerable <OFICINAS> >(listOfOffices); bool retValue = false; using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { await connection.BulkActionAsync(x => { if (offices.Count <OFICINAS>() > 0) { x.BulkDelete(offices); } }); // check who many IEnumerable <OFICINAS> value = await connection.GetAllAsync <OFICINAS>(); retValue = (value.Count <OFICINAS>() == 0); scope.Complete(); } return(retValue); }