public async Task <T> RemoveAsync(T entity, string userId) { var contextEntity = await _context.Set <T>().SingleOrDefaultAsync(p => p.Id == entity.Id); if (contextEntity == null) { throw new Exception($"Entity not found with id {entity.Id}"); } EntityEntry <T> entityEntry; try { // Mark for delete entityEntry = _context.Set <T>().Remove(contextEntity); // Create and add the audit entry var user = await _accountRepository.GetUserOrSystemAsync(userId); await _context.AuditLog.AddAsync(AuditLog.From(entityEntry, AuditAction.DELETE, user)); await _context.SaveChangesAsync(); } catch (Exception e) { throw new Exception("There was an issue saving the entity. See inner exception for more details.", e); } return(entityEntry.Entity); }
public async Task <T> CreateAsync(T entity, string userId) { EntityEntry <T> entityResult; try { // Mark the entity as an add and track entityResult = await _context.Set <T>().AddAsync(entity); // Create and add the audit entry var user = await _accountRepository.GetUserOrSystemAsync(userId); await _context.AuditLog.AddAsync(AuditLog.From(entityResult, AuditAction.CREATE, user)); // Save context changes await _context.SaveChangesAsync(); } catch (Exception e) { throw new Exception("There was an issue saving the entity. See inner exception for more details.", e); } return(entityResult.Entity); }