public async Task <Wagon> UpdateAsync(Wagon wagon) { Wagon actualWagon = _trainContext.Wagons.Include(x => x.Chairs).FirstOrDefault(x => x.WagonId == wagon.WagonId); // Update parent if (actualWagon != null) { _trainContext.Entry(actualWagon).CurrentValues.SetValues(wagon); // Delete children foreach (var existingChild in actualWagon.Chairs.ToList()) { if (!wagon.Chairs.Any(c => c.ChairId == existingChild.ChairId)) { _trainContext.Chairs.Remove(existingChild); } } // Update and Insert children foreach (var childModel in wagon.Chairs) { var existingChild = actualWagon.Chairs .Where(c => c.ChairId == childModel.ChairId && c.ChairId != default) .SingleOrDefault(); if (existingChild != null) { // Update child _trainContext.Entry(existingChild).CurrentValues.SetValues(childModel); } else { // Insert child var newChild = new Chair { ChairId = childModel.ChairId, NearWindow = childModel.NearWindow, Number = childModel.Number, Reserved = childModel.Reserved, WagonId = childModel.WagonId }; actualWagon.Chairs.Add(newChild); } } await _trainContext.SaveChangesAsync(); } return(wagon); }
public IHttpActionResult PutCourse(int id, Course course) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != course.CourseId) { return(BadRequest()); } db.Entry(course).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CourseExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <Train> UpdateAsync(Train train) { Train actualTrain = _trainContext.Trains.Include(x => x.Wagons).FirstOrDefault(x => x.TrainId == train.TrainId); // Update parent if (actualTrain != null) { _trainContext.Entry(actualTrain).CurrentValues.SetValues(train); // Delete children foreach (var existingChild in actualTrain.Wagons.ToList()) { if (!train.Wagons.Any(c => c.WagonId == existingChild.WagonId)) { _trainContext.Wagons.Remove(existingChild); } } // Update and Insert children foreach (var childModel in train.Wagons) { var existingChild = actualTrain.Wagons .Where(c => c.WagonId == childModel.WagonId && c.WagonId != default) .SingleOrDefault(); if (existingChild != null) { // Update child _trainContext.Entry(existingChild).CurrentValues.SetValues(childModel); } else { // Insert child var newChild = new Wagon { WagonId = childModel.WagonId, Chairs = childModel.Chairs, }; actualTrain.Wagons.Add(newChild); } } await _trainContext.SaveChangesAsync(); } return(train); }
public ActionResult Edit([Bind(Include = "DriverTeamID,TeamName,GroupNumber,remark")] DriverTeam driverteam) { try { if (ModelState.IsValid) { db.Entry(driverteam).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } } catch (DataException /* dex */) { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } return(View(driverteam)); }
public ActionResult Edit([Bind(Include = "DriverGroupID,DriverTeamID,DriverGroupName,GroupPeople")] DriverGroup drivergroup) { try { if (ModelState.IsValid) { db.Entry(drivergroup).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } } catch (DataException /* dex */) { ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator."); } ViewBag.DriverTeamID = new SelectList(db.DriverTeams, "DriverTeamID", "TeamName", drivergroup.DriverTeamID); return(View(drivergroup)); }