1

I'm relatively new to this but I'm still a little embarrassed as this really should be simple...

All I'm trying to do is update an existing row in a database table. I'm using EF (5 I believe) code first.

For MVC 3 I used this approach (which worked):

ReportCommon reportcommon = db.ReportCommon.Single(r => r.ReportCommonId == id);
reportcommon.IP = StaticUtilities.GetIPAddress();
db.ObjectStateManager.ChangeObjectState(reportcommon, EntityState.Modified);
db.SaveChanges();

I've tried a few examples that I've found and although they don't error the database doesn't get updated...

[HttpPost]
public ActionResult Edit(CitizenEntryViewModel citizenDetails)
    {
        ActiveCitizen activeCitizen = db.ActiveCitizen.SingleOrDefault(m => m.ID == citizenDetails.ActiveCitizen.ID);

        if (activeCitizen != null)
        {
            citizenDetails.ActiveCitizen.CitizenUpdatedRecordOn = DateTime.Now;
            // Fields we don't edit but still need to pass back
            citizenDetails.ActiveCitizen.PublicID = activeCitizen.PublicID;
            citizenDetails.ActiveCitizen.IsKIN = activeCitizen.IsKIN;
            activeCitizen = citizenDetails.ActiveCitizen;
            db.SaveChanges();
        }
tereško
  • 56,151
  • 24
  • 92
  • 147
ChrisCurrie
  • 1,419
  • 5
  • 15
  • 31
  • There is no need to change the object state to `modified` manually. Is your `IP` property in the model correctly linked to the database? – Silvermind Dec 18 '12 at 11:19
  • Have you tried to change original `activeCitizen` object's properties and save it? Or call `db.ObjectStateManager.ChangeObjectState(activeCitizen, EntityState.Modified);` before `db.SaveChanges();`? – Zabavsky Dec 18 '12 at 11:34
  • Thanks. Silvermind, the first code example was taken from an MVC 3 project some time back. Zabavsky, I did try the approach of changing the state to `modified` but I didn't seem to have access to `ObjectStateManager`. VS Express 2012 says: 'ActiveCitizenSystem.Models.ACSEntities' does not contain a definition for ObjectStateManager. I do have the namespace entry: `using System.Data.Objects;` – ChrisCurrie Dec 18 '12 at 12:18
  • OK, I've just seen that EF 4.0 uses the ObjectContext class where as the version 4.1 uses the DbContext class in which the methods like Set and Entry are defined. As such I have used this syntax: `db.Entry(activeCitizen).State = System.Data.EntityState.Modified;` and now have an error message: "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key." – ChrisCurrie Dec 18 '12 at 12:26

2 Answers2

4

I have managed to resolve this issue using the following code mentioned in this SO post which shows how to save only the new values:

db.Entry(activeCitizen).CurrentValues.SetValues(citizenDetails.ActiveCitizen);

Note, I experienced the error: "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key"

This SO post helped me overcome that issue.

The final code was therefore:

var currentCitizen = db.ActiveCitizen.Find(citizenDetails.ActiveCitizen.ID);
db.Entry(currentCitizen).CurrentValues.SetValues(citizenDetails.ActiveCitizen);
db.SaveChanges();
Community
  • 1
  • 1
ChrisCurrie
  • 1,419
  • 5
  • 15
  • 31
0

Try This

[HttpPost]
public ActionResult Edit(CitizenEntryViewModel citizenDetails)
{
    ActiveCitizen activeCitizen = db.ActiveCitizen.SingleOrDefault(m => m.ID == citizenDetails.ActiveCitizen.ID);

    if (activeCitizen != null)
    {
        UpdateModel(activeCitizen);
        db.SaveChanges();
    }
Niraj
  • 1,542
  • 1
  • 19
  • 32
  • visit Joe Stevens blog to see how update model works. http://www.joe-stevens.com/2010/02/17/asp-net-mvc-using-controller-updatemodel-when-using-a-viewmodel/ – Niraj Dec 18 '12 at 13:03
  • Thanks. I tried the `UpdateModel` approach and received the following error: 'The model of type 'ActiveCitizenSystemMimic.Models.ActiveCitizen' could not be updated.' I have added an approach that works below. Many thanks. – ChrisCurrie Dec 18 '12 at 13:15