public async Task AddEmployee(EmployeeModel emp) { try { _context.Employees.Add(emp); await _context.SaveChangesAsync(); } catch (Exception ex) { throw new Exception("Cannot save data to db!", ex); } }
public async Task<ActionResult> Update(int id, EmployeeModel emp) { try { await _empService.UpdateEmployee(id, emp); return RedirectToAction("Index"); } catch (Exception ex) { ModelState.AddModelError(string.Empty, "Änderungen konnten nicht gespeichert werden!"); } return View(emp); }
public async Task<ActionResult> Create(EmployeeModel emp) { try { if (ModelState.IsValid) { await _empService.AddEmployee(emp); return RedirectToAction("Index"); } } catch (Exception ex) { ModelState.AddModelError(string.Empty, "Änderungen konnten nicht gespeichert werden!"); } return View(emp); }
public async Task UpdateEmployee(int id, EmployeeModel changedEmp) { try { var emp = await _context.Employees.Where(e => e.Id == changedEmp.Id).FirstOrDefaultAsync(); emp.FirstName = changedEmp.FirstName; emp.LastName = changedEmp.LastName; emp.User = changedEmp.User; //emp.Id = id; //_context.Employees.Update(emp); //_context.Employees.Attach(emp); _context.Entry(emp).State = EntityState.Modified; await _context.SaveChangesAsync(); } catch (DbUpdateException ex) { throw new Exception("Cannot save data to db!", ex); } }
public async Task ChangeAssignedEmployee(ApplicationUser user, EmployeeModel newEmployee) { //Den Link entfernen await UnassignUser(user); await AssignUser(newEmployee, user); //var emp = await _context.Employees.Where(e => e.User.Id == user.Id).FirstOrDefaultAsync(); //var newUser = await _context.Users.Where(u => u.Id == user.Id).FirstOrDefaultAsync(); //if (emp != null) //{ // //Link besteht, Employee ändern // emp.User = newUser; // _context.Entry(emp).State = EntityState.Modified; // await _context.SaveChangesAsync(); //} //else //{ // //Link gibt es nicht // newEmployee.User = newUser; // _context.Entry(newEmployee).State = EntityState.Modified; // await _context.SaveChangesAsync(); //} }
public async Task AssignUser(EmployeeModel emp, ApplicationUser user) { ////var link = new EmployeeUser { EmployeeId = emp.EmployeeId, UserId = user.Id }; ////_context.EmployeeUsers.Add(link); ////await _context.SaveChangesAsync(); //using (var db = new ApplicationDbContext()) //{ // //var currEmp = await db.Employees.Where(e => e.Id == emp.Id).FirstOrDefaultAsync(); // //currEmp.User = user; // //db.Entry(currEmp).State = EntityState.Modified; // emp.User = user; // db.Entry(emp).State = EntityState.Modified; // await db.SaveChangesAsync(); //} //Zuerst den User ermitteln var u = await _context.Users.Where(x => x.Id == user.Id).FirstOrDefaultAsync(); emp.User = u; _context.Entry(emp).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (Exception ex) { throw ex; } }
public async Task RemoveEmployee(EmployeeModel emp) { try { _context.Employees.Remove(emp); await _context.SaveChangesAsync(); } catch (DbUpdateException ex) { throw new Exception("Cannot delete data from db!", ex); } }