0

I am trying to delete an object from my database using EntityFramework's DbSet. The code is as follows:

var dbObject = FindById(id);
_masterDb.DbTable.Remove(dbObject);
_masterDb.SaveChanges();

I get the following error: "The object cannot be deleted because it was not found in the ObjectStateManager."

Please can someone tell me what I am doing wrong?

COBOL
  • 1,031
  • 8
  • 16
  • 32
  • 1
    check this one http://stackoverflow.com/questions/15637965/the-object-cannot-be-deleted-because-it-was-not-found-in-the-objectstatemanager – Sefa Oct 10 '14 at 13:26

1 Answers1

2

Possibly the entity is not attached to the same context.

Does this work:

var dbObject = FindById(id);
_masterDb.DbTable.Attach(dbObject);
_masterDb.DbTable.Remove(dbObject);
_masterDb.SaveChanges();
KnottytOmo
  • 546
  • 7
  • 20