public async Task <int> UpdateClient(UpdateClientRequest request) { Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction transaction = _db.Database.BeginTransaction(); ClientEntity client = await _db.Client.FindAsync(request.Id); if (client == null) { throw new ClientNotFoundException(request.Id); } await _db.Client.Where(a => a.Id == request.Id).UpdateAsync(c => new ClientEntity { FirstName = request.FirstName, LastName = request.LastName, SexId = (byte)request.Sex, PersonalNumber = request.PersonalNumber, BirthDate = request.BirthDate, BirthCity = request.City, PhotoUrl = request.PhotoAddress, }); if (request.PhoneNumbers.Any()) { await _db.ClientPhoneNumber.Where(c => c.ClientId == client.Id && !request.PhoneNumbers.Select(p => (byte)p.Type).Contains(c.TypeId)).DeleteAsync(); } await _db.BulkMergeAsync(request.PhoneNumbers.Select(x => new ClientPhoneNumberEntity { ClientId = request.Id, TypeId = (byte)x.Type, Phone = x.Phone }), o => o.ColumnPrimaryKeyExpression = c => new { c.ClientId, c.TypeId }); await transaction.CommitAsync(); return(client.Id); }
public async Task <int> RegisterClient(RegisterClientRequest request) { Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction transaction = _db.Database.BeginTransaction(); ClientEntity client = new ClientEntity { FirstName = request.FirstName, LastName = request.LastName, SexId = (byte)request.Sex, PersonalNumber = request.PersonalNumber, BirthDate = request.BirthDate, BirthCity = request.City, //PhotoUrl = request.PhotoAddress, StatusId = 1 }; await _db.Client.SingleInsertAsync(client); await _db.ClientPhoneNumber.BulkInsertAsync(request.PhoneNumbers.Select(x => new ClientPhoneNumberEntity { ClientId = client.Id, TypeId = (byte)x.Type, Phone = x.Phone })); //await _db.ClientRelation.BulkInsertAsync(request.RelatedClients.Select(x => new ClientRelation { ClientId = client.Id, TypeId = (byte)x.Type, RelativeId = x.Id })); await transaction.CommitAsync(); return(client.Id); }
public async Task <bool> Transaction(Action action) { bool isCommitted = default; Microsoft.EntityFrameworkCore.Storage.IDbContextTransaction transaction = null; try { transaction = AppDbContext.Database.BeginTransaction(); action.Invoke(); SetAudit(); await transaction.CommitAsync(); } catch (Exception exception) { await transaction.RollbackAsync(); Logger.WriteLog(logType: CommonEnum.LogLevelEnum.Error, MethodBase.GetCurrentMethod(), exception: exception); } return(isCommitted); }