The object cannot be deleted because it was not found in the ObjectStateManager<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

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

Posted on: February 15, 2013

It means that the entity is not attached to the database context (DbContext). You need to attach first the entity to the IDbSet of the DbContext. From here, you can remove the entity.

1if (!_set.Local.Contains(entity)) {_set.Attach(entity); }_set.Remove(entity);

That's it! We check if the entity is already attached. If it is, then we do not need to do anything. Otherwise, we attach. Attaching will take the primary key of the entity and delete this one even if it's not fully loaded. The code above could be in a Remove method.