2

I am using EF4.1 to delete an object from my db:

public virtual void Delete(T entity)
{
  _entities.CreateObjectSet<T>().DeleteObject(entity);
}

getting an error:

The object cannot be deleted because it was not found in the ObjectStateManager
user603007
  • 11,416
  • 39
  • 104
  • 168
  • http://stackoverflow.com/questions/449740/error-the-object-cannot-be-deleted-because-it-was-not-found-in-the-objectstatem – Habib Aug 06 '12 at 04:13
  • Solution: http://stackoverflow.com/questions/7791149/the-object-cannot-be-deleted-because-it-was-not-found-in-the-objectstatemanager/19422173#19422173 – Kjartan Oct 17 '13 at 08:58

1 Answers1

4

You get this error if the object you are trying to delete isnt attached to the current context. To get attached objects you either need to query the object from the db on the context, or manually call attach (before delete) to put the object onto the context.

undefined
  • 33,537
  • 22
  • 129
  • 198
  • Thanks big I was getting that error and it was due to querying a list using a different context and then adding objects from that list to be deleted to another context. Didn't realize it was happening. – clamum Dec 15 '22 at 17:03