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

Posted by Sheonarayan under Error and Solution on 5/18/2015 | Points: 10 | Views : 31198 | Status : [Administrator] | Replies : 0

Error

I was getting following error

System.InvalidOperationException: The object cannot be deleted because it was not found in the ObjectStateManager. while trying to delete an object from Entity framework.

Solution

The solution of this problem is to find the object using .where clause and then delete it.

var db = db.Users.Where(u => u.UserName.Equals("myusername")).FirstOrDefault();
db.Users.Delete(db);
db.SaveChanges();

Hope this helps.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com



Responses

(No response found.)

Login to post response