Print Page | Close Window

Invalid ObjectContext on SaveChangesAsync

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2944
Printed Date: 13-May-2026 at 9:25pm


Topic: Invalid ObjectContext on SaveChangesAsync
Posted By: mikke63
Subject: Invalid ObjectContext on SaveChangesAsync
Date Posted: 01-Sep-2011 at 3:52am
Working on a Silverlight application team. Application is for maintaining projects and schedules.
 
When creating a new project, the app starts by creating a root object, then starting a number of async queries to fetch base data etc to control how project is created. As each async query finishes the root project object is updated by creating and attaching sub objects in various referantial collections. All this works well (at least it seams to; no errors are trapped and the correct objects are created and attached).
 
When all async operations are completed and the required objects have been created and attached, we call SaveChangesAsync. Sometimes this goes fine and the object tree is persisted to the database, and sometimes the save operation fails with the following message:
 
EntityManagerSaveException: The object being attached to the source object is not attached to the same ObjectContext as the source object.
 
The funny thing is, we use the exact same parameters on each run, in order to have testing consistency. Each run creates the exact same objects (113 of them every time), and we cannot point at anything that can make it work or fail - seemingly at random. We use only one single entity manager for the whole operation (queries and object creation).
 
To help assist in tracking down the problem we need to answer two questions:
 
1) What is the exact cause of the above exception? The stack trace is as follows:
 
{EntityManagerSaveException: The object being attached to the source object is not attached to the same ObjectContext as the source object. ---> System.InvalidOperationException: The object being attached to the source object is not attached to the same ObjectContext as the source object.
   at System.Data.Objects.DataClasses.RelatedEnd.ValidateEntityForAttach(IEntityWrapper wrappedEntity, Int32 index, Boolean allowCollection)
   at System.Data.Objects.DataClasses.RelatedEnd.Attach(IEnumerable`1 wrappedEntities, Boolean allowCollection)
   at System.Data.Objects.DataClasses.RelatedEnd.System.Data.Objects.DataClasses.IRelatedEnd.Attach(Object entity)
   at IdeaBlade.EntityModel.Edm.EdmSaveExecutor.ProcessManyToMany(List`1 entities)
   at IdeaBlade.EntityModel.Edm.EdmSaveExecutor.ProcessSaves(IEnumerable`1 groupsByType)
   at IdeaBlade.EntityModel.Edm.EdmSaveExecutor.SaveWithinContext()
   at IdeaBlade.EntityModel.Edm.EdmSaveExecutor.Save(DataSourceResolver dataSourceResolver, IDataSourceKey dsKey, SaveWorkState workState)
   ved IdeaBlade.EntityModel.EntityManager.HandleEntityServerException(Exception ex, Boolean tryToHandle, PersistenceOperation operation, Boolean throwOnError)
   ved IdeaBlade.EntityModel.EntityManager.HandleSaveResultException(SaveWorkState saveWorkState, Boolean isAsyncOp)}
 
2) How can we pinpoint the object(s) causing the exception? We have tried - in the SaveChangesAsync callback - looking at the EntitySaveOperation.Exception.EntitiesWithErrors collection, but it is always empty.
 
We are using DevForce for SilverLight version 6.1.2.0.
 
Thanks in advance for any input that can help us navigate in the right direction.
 



Replies:
Posted By: DenisK
Date Posted: 02-Sep-2011 at 11:18am
Hi mikke63;

1. The error is coming from EF. We create an ObjectContext when doing a save (and also when doing a query). We create the context for the specific request and then dispose it. We should not ever have created multiple object contexts for a request. But apparently, somehow in this particular issue of many to many, an Entity A in one end of the relationship seems to be attached to a different ObjectContext in which Entity B of the other end is attached to.

2. The objects causing the exception are the many-to-many objects. Maybe you can start by analyzing those entities first for now and let us know what you find.

If you still can't find anything, we'll probably need to get your edmx, and also a code sample of what you’re doing with the many-to-many entities so we can analyze it.

Here are some links to other pages that may help tracking down the issue.

a. http://msdn.microsoft.com/en-us/library/bb896411(v=vs.90).aspx
b. http://www.ideablade.com/forum/forum_posts.asp?TID=2149&title=is-there-a-multiple-entity-manager-demo
c. http://www.ideablade.com/forum/forum_posts.asp?TID=2549&title=error-during-save




Print Page | Close Window