Unable to save changes to database via Entity Framework 6, have tried a few different totorials but none work

asp.net-mvc-5 c# entity-framework-6

Question

I have tried following a few different tutorials to save updated date to my SQL Database but each fails to save and each one gives different errors, so I am at a complete loss on how I am supposed to update the data. These are what I have tried and the errors I get:

http://msdn.microsoft.com/en-us/library/dd756368.aspx context.UpdateObject does not exist

http://www.asp.net/mvc/tutorials/mvc-5/introduction/examining-the-edit-methods-and-edit-view Give the error

System.Data.Entity.Core.OptimisticConcurrencyException : Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

http://www.dotnetcurry.com/showarticle.aspx?ID=619 give me the error:

Sequence contains no elements

So I am at a loss as what I am supposed to do to update this date. Here are the two code blocks I tried:

[Route("Edit"), HttpPost, ValidateAntiForgeryToken]
public ActionResult EditSection([Bind(Include = "ID, RouteName, Type, Title, Synopsis")]  Section section, HttpPostedFileBase Logo)
{
    SectionAddEditVM model = new SectionAddEditVM { Section = section };

    if (ModelState.IsValid)
    {
        try
        {
            using (db)
            {
                var SectionUpdate = (from s in db.Sections
                                     where s.ID == section.ID
                                     select s).First();
                SectionUpdate.RouteName = section.RouteName;
                SectionUpdate.Type = section.Type;
                SectionUpdate.Title = section.Title;
                SectionUpdate.Synopsis = section.Synopsis;

                db.SaveChanges();
            }
        }
        catch (Exception ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
            ModelState.AddModelError("Processing Error", "There was a problem processing the change, please try again later.");
            return View(model);
        }
    }
    return View(model);
}

AND

[Route("Edit"), HttpPost, ValidateAntiForgeryToken]
public ActionResult EditSection([Bind(Include = "ID, RouteName, Type, Title, Synopsis")] Section section, HttpPostedFileBase Logo)
{
    SectionAddEditVM model = new SectionAddEditVM { Section = section };

    if (ModelState.IsValid)
    {
        try
        {
            using (db)
            {
                db.Entry(section).State = EntityState.Modified;
                db.SaveChanges();
            }
        }
        catch (Exception ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
            ModelState.AddModelError("Processing Error", "There was a problem processing the change, please try again later.");
            return View(model);
        }
    }
    return View(model);
}

About 200 lines above the edit code I have declared my db property using private SectionContext db = new SectionContext(); and it is being used with no issues to add new data to the server and to get data from that server.

Accepted Answer

The solution just came to me, I realized on my view there was no input holding the ID of the section being edited so when it tries to check the DB it has no data to compare to the primary key on. I simply added: @Html.HiddenFor(m => m.Section.ID) and now it all works using:

using (db)
{
    db.Entry(section).State = EntityState.Modified;
    db.SaveChanges();
}

Popular Answer

I had the same problem and adding a line to my input form:

@Html.HiddenFor(m => m.SectionID, model.SectionID) 

Along with adding to the Bind in the controller:

[HttpPost]
public ActionResult Edit([Bind(Include = "SectionID, SectionName, Note")]Section sectionObject)
{
    ...



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why