I have this removal method:
private void btnDeleteOrderLine_Click(object sender, EventArgs e) { OrderLine orderLine = (OrderLine)dgvOrderLine.SelectedRows[0].DataBoundItem; db.OrderLines.Remove(orderLine); db.SaveChanges(); refreshGrid(); }
when I click the delete button, I get this error:
The object cannot be deleted because it was not found in the ObjectStateManager .
I found out that this is because there were two instances of the Context class. So, I tried this:
private void btnDeleteOrderLine_Click(object sender, EventArgs e) { OrderLine orderLine = (OrderLine)dgvOrderLine.SelectedRows[0].DataBoundItem; db.OrderLines.Attach(orderLine);
then it gave me the following error:
An entity object cannot reference multiple instances of IEntityChangeTracker .
How can I fix this and remove the object from the Context DbSet?
source share