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.