public UpdateStatus Update(T updatedEntity) { UpdateStatus upStatus = UpdateStatus.Failed; try { HelpdeskEntity currentEntity = GetByExpression(ent => ent.Id == updatedEntity.Id).FirstOrDefault(); ctx.Entry(currentEntity).OriginalValues["Timer"] = updatedEntity.Timer; ctx.Entry(currentEntity).CurrentValues.SetValues(updatedEntity); if (ctx.SaveChanges() == 1) // should throw exception if stale { upStatus = UpdateStatus.Ok; } } catch (DbUpdateConcurrencyException dbx) { upStatus = UpdateStatus.Stale; Console.WriteLine("Problem in " + GetType().Name + " " + MethodBase.GetCurrentMethod().Name + " " + dbx.Message); } catch (Exception ex) { Console.WriteLine("Problem in " + GetType().Name + " " + MethodBase.GetCurrentMethod().Name + " " + ex.Message); throw ex; } return(upStatus); }
public UpdateStatus Update(T updatedEntity) { UpdateStatus operationStatus = UpdateStatus.Failed; try { Entity currentEntity = GetByExpression(ent => ent.Id == updatedEntity.Id).FirstOrDefault(); _db.Entry(currentEntity).OriginalValues["Timer"] = updatedEntity.Timer; _db.Entry(currentEntity).CurrentValues.SetValues(updatedEntity); if (_db.SaveChanges() == 1) { operationStatus = UpdateStatus.Ok; } } catch (DbUpdateConcurrencyException dbx) { operationStatus = UpdateStatus.Stale; Console.WriteLine("Problem in " + MethodBase.GetCurrentMethod().Name + dbx.Message); } catch (Exception ex) { Console.WriteLine("Problem in " + MethodBase.GetCurrentMethod().Name + ex.Message); } return(operationStatus); }
// Returns Enum value after the update of template public UpdateStatus Update(T updatedEntity) { // Decalre UpdateStatus value UpdateStatus operationStatus = UpdateStatus.Failed; try { // Get the entity that matches the expression HelpdeskEntity currentEntity = GetByExpression(ent => ent.Id == updatedEntity.Id).FirstOrDefault(); _db.Entry(currentEntity).OriginalValues["Timer"] = updatedEntity.Timer; _db.Entry(currentEntity).CurrentValues.SetValues(updatedEntity); // Save information if (_db.SaveChanges() == 1) { operationStatus = UpdateStatus.Ok; } } catch (DbUpdateConcurrencyException dbx) { operationStatus = UpdateStatus.Stale; Console.WriteLine("Problem in " + MethodBase.GetCurrentMethod().Name + dbx.Message); } catch (Exception ex) { Console.WriteLine("Problem in " + MethodBase.GetCurrentMethod().Name + ex.Message); } // try/catch // Returns status return(operationStatus); }
public UpdateStatus Update(T entity) { UpdateStatus opStatus = UpdateStatus.Failed; try { //Retrieve the entity that matches the Id of the parameter argument HelpDeskEntity currentEntity = GetByExpression(emp => emp.Id == entity.Id).FirstOrDefault(); //Set the timer property to be the same as the parameter entity's timer dbContext.Entry(currentEntity).OriginalValues["Timer"] = entity.Timer; //Set the currentEntity's properties to be the same as the parameter entity's properties dbContext.Entry(currentEntity).CurrentValues.SetValues(entity); //If Save Changes is successful, set the enum value to 'Ok' if (dbContext.SaveChanges() == 1) { opStatus = UpdateStatus.Ok; } }catch (DbUpdateConcurrencyException dbx) { //Concurrent updates are happening. Second user has stale data opStatus = UpdateStatus.Stale; Console.WriteLine("Problem in " + MethodBase.GetCurrentMethod().Name + dbx.Message); } catch (Exception ex) { Console.WriteLine("Problem in " + MethodBase.GetCurrentMethod().Name + ex.Message); } //Return the enum value return(opStatus); }
//update employee object public UpdateStatus Update(Employees updatedStudent) { UpdateStatus operationStatus = UpdateStatus.Failed; try { HelpdeskContext _db = new HelpdeskContext(); Employees currentStudent = _db.Employees.FirstOrDefault(stu => stu.Id == updatedStudent.Id); _db.Entry(currentStudent).OriginalValues["Timer"] = updatedStudent.Timer; _db.Entry(currentStudent).CurrentValues.SetValues(updatedStudent); if (_db.SaveChanges() == 1) { operationStatus = UpdateStatus.Ok; } } catch (DbUpdateConcurrencyException) { operationStatus = UpdateStatus.Stale; } catch (Exception ex) { Console.WriteLine("Problem in " + GetType().Name + " " + MethodBase.GetCurrentMethod().Name + " " + ex.Message); throw ex; } return(operationStatus); }
public int Update(Employee updatedEmployee) { int employeesUpdated = -1; try { HelpdeskContext ctx = new HelpdeskContext(); Employee currentStudent = ctx.Employees.FirstOrDefault(Employee => Employee.Id == updatedEmployee.Id); ctx.Entry(currentStudent).CurrentValues.SetValues(updatedEmployee); employeesUpdated = ctx.SaveChanges(); } catch (Exception ex) { Console.WriteLine("Problem in " + GetType().Name + " " + MethodBase.GetCurrentMethod().Name + " " + ex.Message); throw ex; } return(employeesUpdated); }
public int Update(Employees updatedempdent) { int EmployeesUpdated = -1; try { HelpdeskContext _db = new HelpdeskContext(); Employees currentempdent = _db.Employees.FirstOrDefault(emp => emp.Id == updatedempdent.Id); _db.Entry(currentempdent).CurrentValues.SetValues(updatedempdent); EmployeesUpdated = _db.SaveChanges(); } catch (Exception ex) { Debug.WriteLine("Problem in " + GetType().Name + " " + MethodBase.GetCurrentMethod().Name + " " + ex.Message); } return(EmployeesUpdated); }