<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="RSS_xslt_style.asp" version="1.0" ?>
<rss version="2.0" xmlns:WebWizForums="http://syndication.webwiz.co.uk/rss_namespace/">
 <channel>
  <title>DevForce Community Forum : Invalid ObjectContext on SaveChangesAsync</title>
  <link>http://www.ideablade.com/forum/</link>
  <description>This is an XML content feed of; DevForce Community Forum : DevForce 2010 : Invalid ObjectContext on SaveChangesAsync</description>
  <pubDate>Wed, 13 May 2026 21:25:05 -700</pubDate>
  <lastBuildDate>Fri, 02 Sep 2011 11:18:58 -700</lastBuildDate>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Web Wiz Forums 9.69</generator>
  <ttl>360</ttl>
  <WebWizForums:feedURL>www.ideablade.com/forum/RSS_post_feed.asp?TID=2944</WebWizForums:feedURL>
  <image>
   <title>DevForce Community Forum</title>
   <url>http://www.ideablade.com/forum/forum_images/IdeaBlade_logo_tm.png</url>
   <link>http://www.ideablade.com/forum/</link>
  </image>
  <item>
   <title>Invalid ObjectContext on SaveChangesAsync : Hi mikke63;1.The error is coming...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=2944&amp;PID=11621#11621</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=912" rel="nofollow">DenisK</a><br /><strong>Subject:</strong> 2944<br /><strong>Posted:</strong> 02-Sep-2011 at 11:18am<br /><br />Hi mikke63;<div><br></div><div>1.&nbsp;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.</div><div><br></div><div>2.&nbsp;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.</div><div><br></div><div>If you still can't find anything, we'll&nbsp;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.</div><div><br></div><div>Here are some links to other pages that may help tracking down the issue.</div><div><br></div><div><div>a.<span ="Apple-tab-span" style="white-space:pre">	</span>http://msdn.microsoft.com/en-us/library/bb896411(v=vs.90).aspx</div><div>b.<span ="Apple-tab-span" style="white-space:pre">	</span>http://www.ideablade.com/forum/forum_posts.asp?TID=2149&amp;title=is-there-a-multiple-entity-manager-demo</div><div>c.<span ="Apple-tab-span" style="white-space:pre">	</span>http://www.ideablade.com/forum/forum_posts.asp?TID=2549&amp;title=error-during-save</div></div><div><br></div>]]>
   </description>
   <pubDate>Fri, 02 Sep 2011 11:18:58 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=2944&amp;PID=11621#11621</guid>
  </item> 
  <item>
   <title>Invalid ObjectContext on SaveChangesAsync :   Working on a Silverlight application...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=2944&amp;PID=11613#11613</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=1256" rel="nofollow">mikke63</a><br /><strong>Subject:</strong> 2944<br /><strong>Posted:</strong> 01-Sep-2011 at 3:52am<br /><br />Working on a Silverlight application team. Application is for maintaining projects and&nbsp;schedules.<div>&nbsp;</div><div>When creating a new project, the app starts by creating a root object, then&nbsp;starting a number of async queries to&nbsp;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&nbsp;referantial collections. All this works well (at least it seams to;&nbsp;no errors are trapped and the correct objects are created and attached).</div><div>&nbsp;</div><div>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,&nbsp;and sometimes the save operation fails with the following message:</div><div>&nbsp;</div><div><span style='font-family: "Courier New"; font-size: 11pt; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-ansi-: EN-US; mso-fareast-: EN-US; mso-bidi-: AR-SA;' lang="EN-US">EntityManagerSaveException:The object being attached to the source object is not attached to the sameObjectContext as the source object.</span></div><div>&nbsp;</div><div>The funny thing is,&nbsp;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).</div><div>&nbsp;</div><div>To help assist in tracking down the problem we need to answer two questions:</div><div>&nbsp;</div><div>1) What is the exact cause of the above exception? The stack trace is as follows:</div><div>&nbsp;</div><div><font face="Courier New, Courier, mono">{EntityManagerSaveException: The object being attached to the source object is not attached to the same ObjectContext as the source object. ---&gt; System.InvalidOperationException: The object being attached to the source object is not attached to the same ObjectContext as the source object.<br>&nbsp;&nbsp; at System.Data.Objects.DataClasses.RelatedEnd.ValidateEntityForAttach(IEntityWrapper wrappedEntity, Int32 index, Boolean allowCollection)<br>&nbsp;&nbsp; at System.Data.Objects.DataClasses.RelatedEnd.Attach(IEnumerable`1 wrappedEntities, Boolean allowCollection)<br>&nbsp;&nbsp; at System.Data.Objects.DataClasses.RelatedEnd.System.Data.Objects.DataClasses.IRelatedEnd.Attach(Object entity)<br>&nbsp;&nbsp; at IdeaBlade.EntityModel.Edm.EdmSaveExecutor.ProcessManyToMany(List`1 entities)<br>&nbsp;&nbsp; at IdeaBlade.EntityModel.Edm.EdmSaveExecutor.ProcessSaves(IEnumerable`1 groupsByType)<br>&nbsp;&nbsp; at IdeaBlade.EntityModel.Edm.EdmSaveExecutor.SaveWithinContext()<br>&nbsp;&nbsp; at IdeaBlade.EntityModel.Edm.EdmSaveExecutor.Save(DataSourceResolver dataSourceResolver, IDataSourceKey dsKey, SaveWorkState workState)<br>&nbsp;&nbsp; ved IdeaBlade.EntityModel.EntityManager.HandleEntityServerException(Exception ex, Boolean tryToHandle, PersistenceOperation operation, Boolean throwOnError)<br>&nbsp;&nbsp; ved IdeaBlade.EntityModel.EntityManager.HandleSaveResultException(SaveWorkState saveWorkState, Boolean isAsyncOp)}</font></div><div>&nbsp;</div><div>2)&nbsp;How can we pinpoint the object(s) causing the exception? We have tried - in the&nbsp;SaveChangesAsync callback - looking at the&nbsp;EntitySaveOperation.Exception.EntitiesWithErrors collection, but it is always empty.</div><div>&nbsp;</div><div>We are using DevForce&nbsp;for SilverLight&nbsp;version 6.1.2.0.</div><div>&nbsp;</div><div>Thanks in advance for any input that can&nbsp;help us navigate in the right direction.</div><div>&nbsp;</div>]]>
   </description>
   <pubDate>Thu, 01 Sep 2011 03:52:59 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=2944&amp;PID=11613#11613</guid>
  </item> 
 </channel>
</rss>