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?
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With