public void Remove(VacationDto dto) { if (DeleteEmployeeIfEmpty(dto)) { return; } Vacation vacation = new Vacation { ID = dto.ID, RowVersion = dto.RowVersion }; _context.Vacations.Attach(vacation); _context.Entry(vacation).State = EntityState.Deleted; }
private bool SetExistingEmployeeByName(Vacation vacation, VacationDto dto) { Employee employee = _context.Employees.SingleOrDefault(x => x.ID != dto.EmployeeID && x.FirstName == dto.EmployeeFirstName && x.LastName == dto.EmployeeLastName); if (employee == null) { return(false); } _context.Entry(vacation.Employee).State = EntityState.Detached; vacation.EmployeeID = employee.ID; vacation.Employee = employee; dto.EmployeeOwnerID = employee.OwnerID; return(true); }
public void Edit(VacationDto dto) { Vacation vacation = _mapper.Map <Vacation>(dto); _context.Vacations.Attach(vacation); _context.Entry(vacation).State = EntityState.Modified; if (_context.Employees.Any(x => x.ID == dto.EmployeeID && (x.FirstName != dto.EmployeeFirstName || x.LastName != dto.EmployeeLastName))) { if (SetExistingEmployeeByName(vacation, dto)) { DeleteEmployeeIfEmpty(dto); } else { _context.Entry(vacation.Employee).State = EntityState.Modified; } } }