public IHttpActionResult PutPerson(int id, Person person) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != person.PersonID) { return(BadRequest()); } db.Entry(person).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!PersonExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
/// <summary> /// Removes test data from database, called from all tests that create data /// </summary> /// <remarks> /// If you have issues with data not disposing then set break-points /// in the emppty try/catch statements to figure out the issue. More likely /// than not the interface, in this case IBaseEntity was not implemented on /// one of the classes. /// /// The try-catches allow us to continue and throw an exception message in /// the tear down event TeardownTestBase for any test. /// /// Empty try/catches are okay here as you should be using this only for /// unit testing and hopefully on a non production database. /// /// </remarks> public bool AnnihilateData(List <object> mAnnihilateList) { bool mAnnihilateDataSuccessful = false; using (var destroyContext = new PersonEntities()) { for (int i = mAnnihilateList.Count - 1; i >= 0; i--) { try { var currentObject = mAnnihilateList[i]; var existingItem = destroyContext .Set(currentObject.GetType()) .Find(((IBaseEntity)currentObject).Identifier); if (existingItem != null) { try { var attachedEntry = destroyContext.Entry(existingItem); attachedEntry.CurrentValues.SetValues(currentObject); destroyContext.Set(existingItem.GetType()).Attach(existingItem); destroyContext.Set(existingItem.GetType()).Remove(existingItem); } catch (Exception) { // ignore nothing do to as the object was not added in properly. } } else { var item = currentObject.GetType(); } } catch (Exception) { //catch and continue save what we can } } try { var resultCount = destroyContext.SaveChanges(); var annihlationCount = mAnnihilateList.Count; mAnnihilateDataSuccessful = (resultCount == annihlationCount); } catch (Exception) { // keep on going } finally { destroyContext.Dispose(); } } return(mAnnihilateDataSuccessful); }
public ActionResult Edit([Bind(Include = "PersonId,LastName,FirstName")] Person person) { if (ModelState.IsValid) { db.Entry(person).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(person)); }
public ActionResult Edit([Bind(Include = "IdPerson,name,lastname,BDate,Photo,Describe,Gender,LostDate,LostPlace")] LostPerson lostPerson) { if (ModelState.IsValid) { db.Entry(lostPerson).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.Gender = new SelectList(db.Gender, "Id", "Name", lostPerson.Gender); return(View(lostPerson)); }