Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The object cannot be deleted because it was not found in the ObjectStateManager

Tags:

I have this code which normally works:

db.myTable.DeleteObject(myCurrent); 

And I got this error:

The object cannot be deleted because it was not found in the ObjectStateManager. 

The same ingredients IS in the table in the database.

I tried this:

db.myTable.Attach(myCurrent); db.myTable.DeleteObject(myCurrent); 

And I got another error:

An entity object cannot be referenced by multiple instances of IEntityChangeTracker.  

How to fix this?

like image 829
petko_stankoski Avatar asked Apr 11 '13 09:04

petko_stankoski


1 Answers

The problem is you cannot delete (or remove) detached entities and cannot attach an entity twice. You need something like below.

var entry = db.Entry(myCurrent); if (entry.State == EntityState.Detached)     db.myTable.Attach(myCurrent); db.myTable.Remove(myCurrent); 
like image 146
Mehmet Ataş Avatar answered Sep 21 '22 15:09

Mehmet Ataş