0

i have the following EdiePOST action method:-

[HttpPost]
        public ActionResult Edit([Bind(Include = "Note,DoctorID,VisitID,StatusID,timestamp")] Visit visit) //[Bind(Include="Note,DoctorID,VisitID,StatusID")]
        {
            if ((visit.EditableByAssingedDoctor(User.Identity.Name)) || (visit.EditableByCreatedBy(User.Identity.Name)))
            {

                try
                {
                    if (ModelState.IsValid)
                    {

                        int id = visit.VisitID;
                        var v = repository.GetVisit(id);
                        visit.CreatedBy = v.CreatedBy;
                        visit.Date = v.Date;
                        visit.PatientID = v.PatientID;
                        visit.VisitTypeID = v.VisitTypeID;


                        repository.UpdateVisit(visit);
                        repository.Save();
                        return RedirectToAction("Index");
                    }
                }
                catch (DbUpdateConcurrencyException ex)
                {
//code goes here

where the repository.UpdateVisit(visit);is :-

 public void UpdateVisit(Visit v)
            {
                entities.Entry(v).State = EntityState.Modified;

            }

But when i run my application and i try to edit the visit object i got the following error :-

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

on the repository.UpdateVisit(visit); method,, so what are going wrong? BR

John John
  • 1
  • 72
  • 238
  • 501
  • 1
    Couldn't tell you if this is causing your error, but it looks like you should be setting the variable v equal to the variable visit and then updating v instead of doing it the other way around. – DMulligan Apr 29 '12 at 21:23

1 Answers1

1

You are attaching two objects with the same key to the same context: v in repository.GetVisit(id) and visit in repository.UpdateVisit(visit). This causes the exception.

Since you already load the entity from the database you can just update its properties and then save it. Instead of using...

repository.UpdateVisit(visit);

...use...

repository.UpdateAttachedVisit(v, visit);

...with:

public void UpdateAttachedVisit(Visit attachedVisit, Visit detachedVisit)
{
    entities.Entry(attachedVisit).CurrentValues.SetValues(detachedVisit);
}
Slauma
  • 175,098
  • 59
  • 401
  • 420
  • thanks for the reply, i tried ur code but it raised the following exception "System.Data.Entity.Validation.DbEntityValidationException was unhandled by user code Message=Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. Source=EntityFramework" – John John Apr 29 '12 at 23:46
  • @johnG: That's another error which has nothing to do with the first problem. You need to inspect the `EntityValidationErrors` as the message said, to find out the problem - either in debugger, or similar to this: http://stackoverflow.com/a/7798264/270591 – Slauma Apr 30 '12 at 00:02