1

I am getting the following error when I try to delete an object from the datase using LINQ.

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

Note: When I try to run the query directly from the database, the object is deleted.

Code where I am getting the error:

 public void deleteStorage(CommonLayer.TblNewsStorage storageToDelete)
    {
        using(DBTicketSystemEntities e = new DBTicketSystemEntities())
        {
            e.TblNewsStorage.DeleteObject(storageToDelete);
            e.SaveChanges();
        }
    }

Stack Trace:

    [InvalidOperationException: The object cannot be deleted because it was not found in the ObjectStateManager.]
   System.Data.Objects.ObjectContext.DeleteObject(Object entity, EntitySet expectedEntitySet) +3061568
   System.Data.Objects.ObjectSet`1.DeleteObject(TEntity entity) +18
   DataLayer.DAStorage.deleteStorage(TblNewsStorage storageToDelete) in C:\Users\Eric\Desktop\Development\Library News\LibraryNews\DataLayer\DAStorage.cs:38
   BusinessLayer.Storage.deleteStorage(String id) in C:\Users\Eric\Desktop\Development\Library News\LibraryNews\BusinessLayer\Storage.cs:43
   NewsLibrary.Controllers.StorageController.Delete(TblNewsStorage storage) in C:\Users\Eric\Desktop\Development\Library News\LibraryNews\NewsLibrary\Controllers\StorageController.cs:61
   lambda_method(Closure , ControllerBase , Object[] ) +180
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +208
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +28
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +48
   System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +57
   System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +223
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +48
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +24
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9688704
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
rikket
  • 2,357
  • 7
  • 46
  • 74

2 Answers2

2

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();
    }
}
Ramy M. Mousa
  • 5,727
  • 3
  • 34
  • 45
1

try below code:-

e.TblNewsStorage(storageToDelete).State = System.Data.EntityState.Deleted;
e.SaveChanges();

OR

 e.TblNewsStorage.Attach(storageToDelete);
 e.TblNewsStorage.Remove(storageToDelete);
 e.SaveChanges();
Neel
  • 11,625
  • 3
  • 43
  • 61