First of all, I'm a just beginner, when I trying to delete a record in sql by passing id through view to controller, below is my code:
Note: 'username' is the PK in table "T_Users"
#region Delete record function
public ActionResult Delete(string id ) {
T_Users temp_u = new T_Users() { Username = id };
//db.T_Users.Attach(new T_Users() { Username = id });
db.T_Users.Remove(temp_u);
try
{
db.SaveChanges();
//return View();
}
catch(Exception e) {
return Content("hehe");
}
return RedirectToAction("Index");
}
#endregion
When I call the T_Users.Attach() method, it said "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key"
Then I commented that, it failed because "The object cannot be deleted because it was not found in the ObjectStateManager."
Can anybody offer some ideas about the solution?