This project is read-only.

"The object is in a detached state" error

Feb 8, 2013 at 2:31 PM
Hi,

I received the error message below in my application

The object is in a detached state. This operation cannot be performed on an ObjectStateEntry when the object is detached.

I'm trying to insert a new record into a view which are based on two tables. I have a instead of trigger created for this view, this trigger will insert new record into each of this two tables.

I'm using Container.NewTransientInstance() method to create the object and then call Container.Persist(ref recordedAction) to persist the object. I understand container has the responsibility to track object state.
The error was NOT thrown when the persist method called. But the error was thrown on this line

NakedObjects.Web.Mvc.Controllers.GenericControllerImpl.OnActionExecuted(ActionExecutedContext filterContext) +84


Any idea what would cause that issue. Thanks.




Details Error messages:



Stack Trace:

[InvalidOperationException: The object is in a detached state. This operation cannot be performed on an ObjectStateEntry when the object is detached.]
System.Data.Objects.EntityEntry.get_IsRelationship() +2152712
NakedObjects.EntityObjectStore.EntityObjectStore.<GetSourceNakedObject>b__5b(ObjectStateEntry e) +59
System.Linq.WhereSelectEnumerableIterator2.MoveNext() +204
System.Linq.Enumerable.SingleOrDefault(IEnumerable
1 source) +4077101
NakedObjects.EntityObjectStore.EntityObjectStore.GetSourceNakedObject(OptimisticConcurrencyException oce) +264
NakedObjects.EntityObjectStore.EntityObjectStore.EndTransaction() +599
NakedObjects.Persistor.Objectstore.ObjectStoreTransaction.Commit() +208
NakedObjects.Persistor.Objectstore.ObjectStoreTransactionManager.EndTransaction() +101
NakedObjects.Persistor.Objectstore.ObjectStorePersistor.EndTransaction() +65
NakedObjects.Web.Mvc.Controllers.NakedObjectsController.OnActionExecuted(ActionExecutedContext filterContext) +103
NakedObjects.Web.Mvc.Controllers.GenericControllerImpl.OnActionExecuted(ActionExecutedContext filterContext) +84
System.Web.Mvc.Controller.System.Web.Mvc.IActionFilter.OnActionExecuted(ActionExecutedContext filterContext) +9
System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +251
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult1.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.WrappedAsyncResult1.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() +9629708
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
Feb 8, 2013 at 3:10 PM
"The error was NOT thrown when the persist method called. But the error was thrown on this line ... NakedObjects.Web.Mvc.Controllers.GenericControllerImpl.OnActionExecuted(ActionExecutedContext filterContext) +84"

The line on which the error is thrown is a red herring, Xiao. That's just because the controller method defines the transaction boundaries - so the attempt to persist doesn't actually occur until the end of the transaction - you may think (in debug mode) that you successfully got past the Persist, but you haven't actually persisted at that point.

Detached means an object that EF has not yet seen, proxied, and kept a reference to.

The most common cause of the error you are seeing is when you are persisting more than one object in a transaction, and they are associated in a non-simple way. If the two objects have a simple 1:n association then there is usually no problem. But if there are other associations involved then you can get issues with trying to persist an object with associations to objects not yet persisted. I'd need to look at the actual code to see if this was the case here.
Feb 8, 2013 at 3:55 PM
Thanks very much Richard. I'll investigate on that.