The Object Cannot Be Deleted Because It Was Not Found in the Objectstatemanager

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

You can fetch the row from the database and then delete it, but this causes 2 round trips to the database.

If you want to do it in one hit, your second version with Attach will work - as long as the entity is not already loaded into the context.

The error you are getting is caused by EF validation which runs before any database write.

You can turn it off temporarily like this:

bool oldValidateOnSaveEnabled = localDb.Configuration.ValidateOnSaveEnabled;

try
{
localDb.Configuration.ValidateOnSaveEnabled = false;

var customer = new Customer { CustomerId = id };

localDb.Customers.Attach(customer);
localDb.Entry(customer).State = EntityState.Deleted;
localDb.SaveChanges();
}
finally
{
localDb.Configuration.ValidateOnSaveEnabled = oldValidateOnSaveEnabled;
}

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

The other answer didn't work, so here's how I fixed it.

Previously I had:

public void ok(myTable myCurrent)
{
//delete entries from other tables in relationship with myTable
db.myTables.DeleteObject(myCurrent);

}

I fixed it with this:

public void ok(int current_id)
{
//delete entries from other tables in relationship with myTable
var y = (from x in db.myTables where x.id == current_id select x).First();
db.myTables.DeleteObject(y);

}

MVC5/EF6: Object cannot be deleted because it was not found in the ObjectStateManager?

The problem is that you're actually using 2 DbContext here and they cannot track each other's Entities.

You have to change your Controller code to have both db and UserManager share the same context instance reference:

public UserManagementController()
{
this.db = context.Get<ApplicationDbContext>();
}

Unable to fix 'The object cannot be deleted because it was not found in the ObjectStateManager.'

Try sending the ID of the object you want to delete then query it inside your context instead of sending the whole object :

    public void deleteStorage(int ID)
{
using(DBTicketSystemEntities e = new DBTicketSystemEntities())
{
//Like That
var selectedItem = e.TblNewsStorage.Where( t => t.ID == ID).FirstOrDefault();
e.TblNewsStorage.DeleteObject(selectedItem);
e.SaveChanges();
}
}

The object cannot be deleted because it was not found in the ObjectStateManager error when deleting item

RoleManager.Delete calls RoleStore.Delete which finally call DbSet<Role>.Remove(role). As it's already mentioned in DbSet<T>.Remove(T), the that you pass to the entity must exist in the context in some other state before this method is called.

Obviously the role instance which you create using new Role() doesn't exists in the context. So the following line of code will throw an exception:

var r = new Role(){ ... };
manager.Delete(r); // ← throws exception

Above description also describes why your edited code works, because you load a role into context, then try to delete that attached (tracked) role.

If you would like to reuse the Get method which you create, the method should return Role or have a reference to the Role in the return value. Then that reference (which has been attached to EF context) can be easily deleted by manager, because it's been tracked.

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

Each EF object is tightly associated to the manager (for want of a better word) that created it. or to which it has been associated. Since you don't pass db to your Get method, I assume that Get has either used it's own J1Entities, or the object has been created standalone (perhaps deserialized).

In order to delete it, it must know about it first. That might mean by attaching an object to the manager - but in this case, it seems like an easier option is just to pass db into Get, so that the Get is done in the same context (since db will automatically attach objects that it creates itself).



Related Topics



Leave a reply



Submit