// Update //originalEntity represents the object before the change (usually fetched from database before you update). //It must be attached to the context. changedEntity represents the entity with the same key which has been changed. public static void UpdateProject(proj_info proj_info, Func <proj_info, int> getKey) { // _ctx.Entry(proj_info).CurrentValues.SetValues(proj_info); if (proj_info == null) { throw new ArgumentException("Cannot add a null entry"); } // To get information about an entity in question, and it's relation to the current DbContext, you call a DbContext.Entry method var entry = _ctx.Entry(proj_info); // Debug.Write(entry.State); //EntityState.Detached if (entry.State == EntityState.Detached) { var set = _ctx.Set <proj_info>(); proj_info attachedEntity = set.Find(getKey(proj_info)); if (attachedEntity != null) { var attachedEntry = _ctx.Entry(attachedEntity); attachedEntry.CurrentValues.SetValues(proj_info); } else { entry.State = EntityState.Modified; // this should attached the entry } } }
// [HttpPost] public ActionResult UpdateProject(proj_infoViewModel info, int id) { // this is how you access the list of errors that can occur during the model binding. if (!ModelState.IsValid) { return(View(info)); } var p = projectInfo.GetProjectInfo(id); if (p == null) { return(RedirectToAction("Index")); } string projDesc = info._proj_name; var pi = new proj_info { proj_info_id = id, proj_name = info._proj_name, proj_mgr_id = info._proj_mgr_id, proj_sponsor_id = info._proj_sponsor_id, charter = info._charter, scope_definition = info._scope_definition, dept_id = info._dept_id, client = info._client, proj_start_date = info._proj_start_date.Value, proj_end_date = info._proj_end_date, proj_status_id = info._proj_status_id, project_type_id = info.proj_type_id, createdby = User.Identity.Name.Replace("GREENSPOONMARDE\\", ""), createddate = DateTime.Now, notes = info._notes, proj_goal = info._proj_goal, doc_location = info._doc_location }; var existingProjInfo = _ctx.proj_info.Find(pi.proj_info_id); if (existingProjInfo != null) { // entity already in the context var attachedEntry = _ctx.Entry(existingProjInfo); attachedEntry.CurrentValues.SetValues(pi); } _ctx.SaveChanges(); TempData["ConfirmationMessage"] = pi.proj_name + " has been updated."; // I want to redirect here instead of just letting the users sit on the save values on that posted form fields, it is very common after HTTP POST you redirect them back to a page where they // can view the changed results, THAT WAY, they don't hit refresh on the result of this post operation and accidentally submit something twice. return(RedirectToAction("Index", new { searchTerm = projDesc })); }
public static void UpdateProject(proj_info proj_info) { using (var db = new PMEntities()) { var existingProjInfo = db.proj_info.Find(proj_info.proj_info_id); if (existingProjInfo != null) { // entity already in the context var attachedEntry = db.Entry(existingProjInfo); attachedEntry.CurrentValues.SetValues(proj_info); } else { // Since we don't have it in db, this is a simple add. db.proj_info.Add(proj_info); } } }