من اکشن رو به اینصورت تغییر دادم .


public ActionResult Edit([Bind(Include = "PersonId,PersonCode,PersonName,PersonFamily")] People people)
{
if (ModelState.IsValid)
{
var person = db.People.Find(people.PersonId);
if (person.PersonCode==people.PersonCode)
{
db.Entry(people).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
else
{
var personCode = db.People.SingleOrDefault(c => c.PersonCode == people.PersonCode);
if (personCode!=null)
{
ModelState.AddModelError("PersonCode", "کد وارد شده تکراری می باشد");
}
else
{
db.Entry(people).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
}
return View(people);
}


ولی موقع ویرایش ارور میده

System.InvalidOperationException: 'Attaching an entity of type 'DataLayer.People' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values.