Reputation: 107
I am trying to delete rows from a table using this code. But getting error.
MyEntity _er = new MyEntity();
List<result> V_Result= R_Enrollment.GetResult(V_studentid, DR.Cells[0].Value.ToString() );
foreach(var lop in V_Result)
{
_er.results.Remove(lop);
}
_er.SaveChanges();
_er.Dispose();
getting the following error on _er.results.Remove(lop);
ex = {"The object cannot be deleted because it was not found in the ObjectStateManager."}
Upvotes: 1
Views: 2456
Reputation: 4657
Your _er object doesn't know about the items you brought back from that other function call. You must first attach the objects and then delete them.
Here's what should be inside your loop, assuming the lop object has the primary key of the record:
_er.Attach(lop);
_er.results.Remove(lop);
Upvotes: 2