Hi I'm getting following error while updating the record
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
Here's my Edit (Post) Action.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,Name,Type,Weightage,Description,ParentId")] HiringFactor hiringfactor)
{
if (ModelState.IsValid)
{
var childCount = 0;
var indent = "";
var parentId = hiringfactor.ParentId;
HiringFactor parentFactor = db.HiringFactors.Find(parentId);
if (parentFactor != null)
{
childCount = parentFactor.ChildHiringFactors.Count;
indent = parentFactor.Indent + "." + (childCount + 1);
}
else
{
var parentCount = db.HiringFactors.Count(hf => (hf.ParentId == null || hf.ParentId == 0));
indent = (parentCount + 1).ToString();
}
hiringfactor.Indent = indent;
db.Entry(hiringfactor).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ParentFactors = new SelectList(db.HiringFactors, "Id", "IndentName", hiringfactor.ParentId);
return View(hiringfactor);
}
Is it becaus of I'm dealing with objects of Same Type within DB Context?