<?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 : AttachEntity and AddEntity</title>
  <link>http://www.ideablade.com/forum/</link>
  <description>This is an XML content feed of; DevForce Community Forum : DevForce 2009 : AttachEntity and AddEntity</description>
  <pubDate>Tue, 28 Apr 2026 18:11:41 -700</pubDate>
  <lastBuildDate>Fri, 21 Aug 2009 16:41:36 -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=1439</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>AttachEntity and AddEntity : Another, important difference...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=1439&amp;PID=5228#5228</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=482" rel="nofollow">WardBell</a><br /><strong>Subject:</strong> 1439<br /><strong>Posted:</strong> 21-Aug-2009 at 4:41pm<br /><br /><P>Another, important difference between AttachEntity and <strong>ImportEntities</strong>: </P><DIV>You can only import entities which already belong to an EntityManager (i.e., <U>are not "Detached"</U>). This means that</DIV><DIV>&nbsp;</DIV><DIV><FONT face="Courier New, Courier, mono">&nbsp; the<FONT size=2>EntityManager.Imports(new &#091;&#093; {theEntity})</FONT> </FONT></DIV><DIV>&nbsp;</DIV><DIV>will throw an exception if "theEntity" is a detached,&nbsp;hand-rolled stub (as of v.5.2.2). </DIV><DIV>&nbsp;</DIV><DIV>Import still fulfills admirably its primary purpose&nbsp;which is&nbsp;to copy entities from one EntityManager to another.</DIV>]]>
   </description>
   <pubDate>Fri, 21 Aug 2009 16:41:36 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=1439&amp;PID=5228#5228</guid>
  </item> 
  <item>
   <title>AttachEntity and AddEntity : We are addingan AttachEntity(object...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=1439&amp;PID=5227#5227</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=482" rel="nofollow">WardBell</a><br /><strong>Subject:</strong> 1439<br /><strong>Posted:</strong> 21-Aug-2009 at 3:15pm<br /><br /><P>We are adding&nbsp;an <strong>AttachEntity(object entity)</strong>&nbsp;method to the EntityManager in release 5.2.2. </P><DIV>Those of you who write test and don't want those tests to touch the database will appreciate this the most.</DIV><DIV>&nbsp;</DIV><DIV>As you know, you sometimes need to write tests which rely upon interaction with the EntityManager. You want to populate a disconnected EntityManager with a small collection of hand-rolled stub entities. While such tests are integration tests because they rely on a dependency, we still want to make them easy to write and we want them to be fast. That means we don't want a trip to a database when we run them; we shouldn't need to <EM>have</EM> a database to run them.</DIV><DIV>&nbsp;</DIV><DIV>I usually start by creating a test-oriented, disconnected&nbsp;EntityManager ... which can be as simple as</DIV><DIV>&nbsp;</DIV><DIV><FONT size=2 face="Courier New, Courier, mono">&nbsp;&nbsp; var testManager = new EntityManager(false <FONT color=#008000>/* disconnected */ </FONT>);</FONT></DIV><DIV>&nbsp;</DIV><DIV>The easiest way to get a stub entity is to&nbsp;"new" it up, set some of its properties, give it an EntityKey, and dump it in&nbsp;our testManager.&nbsp;when we're done it&nbsp;should appear there as an unchanged entity ...&nbsp;as if you had&nbsp;read it from the datastore.</DIV><DIV>&nbsp;</DIV><DIV>The catch is "how do I add the entity to the manager?"</DIV><DIV>&nbsp;</DIV><DIV>In the absence of&nbsp;AttacheEntity you&nbsp;are likely to have used&nbsp;EntityManager.<strong>AddEntity</strong>. But&nbsp;after AddToManager, the EntityState of the entity&nbsp;is always "Added".&nbsp;You want "Unchanged"&nbsp;so you have to remember to call AcceptChanges (which changes the state to "Unchanged").</DIV><DIV>&nbsp;</DIV><DIV>That's not too hard. Unfortunately, it gets messy if the key of the entity is auto-generated (e.g., mapped to a table whose id field is auto-increment) because DevForce automatically replaces your key with a temporary one as part of its auto-id-generation behavior.</DIV><DIV>&nbsp;</DIV><DIV>I could explain how to work around this. What a PITA. We really just want a simple way to simulate the result of retrieving an entity. That's why we now have AttachEntity.</DIV><DIV>&nbsp;</DIV><DIV>Here's the XML documentation for AttachEntity:</DIV><DIV>&nbsp;</DIV><DIV><P style="MARGIN: 0in 0in 0pt" =Ms&#111;normal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt">Adds a detached entity to this EntityManager in an Unmodified state.&nbsp; </P><DIV>Throws an exception if an entity with the same key already exists in the manager</DIV><DIV>of if the specified entity is not in a detached state.</DIV></SPAN></DIV><DIV>&nbsp;</DIV><DIV>Let me elaborate here and compare it to some similar methods by calling out the following facts about the following code fragment:</DIV><DIV>&nbsp;</DIV><DIV><SPAN style="FONT-FAMILY: 'Calibri','sans-serif'; COLOR: #1f497d; 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"><FONT size=3 face="Courier New, Courier, mono">&nbsp;&nbsp; theEntityManager.AttachEntity(object theEntity)</FONT></SPAN></DIV><DIV>&nbsp;</DIV><DIV><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>1.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>theEntity’s EntityKey (“the key”) must be preset prior to the attach operation which will not touch the key<?: prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></FONT></FONT></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>2.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>An exception is thrown if an entity with that key is already in the cache<o:p></o:p></FONT></FONT></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>3.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>After attach, theEntity is in an “Unchanged” EntityState (“the state”)<o:p></o:p></FONT></FONT></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>4.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>theEntity is presumed to exist in the persistent store; &nbsp;subsequent change and save will translate to an update statement.<o:p></o:p></FONT></FONT></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>5.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>After successful attach, a reference to theEntity is a reference to the entity with that key in the manager’s EntityCache; contrast with <FONT size=2 face="Courier New, Courier, mono">anEntityManager.Imports(new &#091;&#093; {anEntity})</FONT>” as discussed below.</FONT></FONT></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>6.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>theEntity must be in the “Detached” state prior to the operation<o:p></o:p></FONT></FONT></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>7.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>An exception is thrown if theEntity is other than in “Detached” state prior to the operation<o:p></o:p></FONT></FONT></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>8.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>After attach, related entities are implicitly associated with theEntity automatically; for example, if anOrder with Id==22 is attached and there are OrderDetails with parent OrderId==22, then after the attach, anOrder.OrderDetails returns these details and any one of them will return ‘anOrder’ in response to anOrderDetail.Order.<o:p></o:p></FONT></FONT></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>9.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>The sequence of attachments is not important; OrderDetails may be added prior to the parent Order;<o:p></o:p></FONT></FONT></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>10.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>Attach has no effect on theEntityManager’s QueryCache<o:p></o:p></FONT></FONT></P><P style="MARGIN: 0in 0in 0pt" =Ms&#111;normal><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></P><P style="MARGIN: 0in 0in 0pt" =Ms&#111;normal><FONT size=3><FONT face=Calibri>AttachEntity and <strong>AddEntity</strong> behave the same way <B><I>except</I></B>:<o:p></o:p></FONT></FONT></P><P style="MARGIN: 0in 0in 0pt" =Ms&#111;normal><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>11.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>After add, theEntity is in an “Added” state<o:p></o:p></FONT></FONT></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>12.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>theEntity is presumed to be new and to be absent from in the persistent store; a save will translate to an insert statement.<o:p></o:p></FONT></FONT></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>13.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>If the key for this type is auto-generated (e.g., backed by an auto-increment field in the database), the existing key will be set to a generated temporary key, replacing the prior key value.<o:p></o:p></FONT></FONT></P><P style="MARGIN: 0in 0in 0pt 2.25pt" =Ms&#111;normal><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></P><P style="MARGIN: 0in 0in 0pt 2.25pt" =Ms&#111;normal><FONT size=3><FONT face=Calibri>The following is true regarding <strong>detaching</strong> anEntity:<o:p></o:p></FONT></FONT></P><P style="MARGIN: 0in 0in 0pt 2.25pt" =Ms&#111;normal><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>14.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>After detach, anEntity enters the “Detached” state no matter what its prior state.<o:p></o:p></FONT></FONT></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>15.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>Detaching an Order does not detach its child OrderDetails; they remain “orphaned” in the cache<o:p></o:p></FONT></FONT></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>16.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>The sequence of detachments is not important; an Order may be detached prior to detaching its child OrderDetails<o:p></o:p></FONT></FONT></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>17.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>Detach has no effect on theEntityManager’s QueryCache</P><DIV></DIV><o:p><P style="MARGIN: 0in 0in 0pt 2.25pt" =Ms&#111;normal><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></P><P style="MARGIN: 0in 0in 0pt 2.25pt" =Ms&#111;normal><FONT size=3><FONT face=Calibri><strong>EntityManager.Imports</strong> is another way of populating an EntityManager with a collection of entities that may have come from anywhere (including hand-rolled). Here's how you might "import" a single stub entity:</P><DIV>&nbsp;&nbsp;&nbsp; the<FONT size=2 face="Courier New, Courier, mono">EntityManager.Imports(new &#091;&#093; {theEntity})</FONT> </DIV><DIV>&nbsp;</DIV><DIV>Imports&nbsp;differs from AttachEntity in that:</DIV><DIV></DIV><P style="MARGIN: 0in 0in 0pt 2.25pt" =Ms&#111;normal><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>18.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp; <FONT size=3 face=Calibri>it requires a MergeStrategy to tell it what to do if an entity with the same key as&nbsp;"theEntity" already exists in the cache.</FONT></SPAN></SPAN></SPAN></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore">19.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;<FONT size=3 face=Calibri>it merges "theEntity" into the cache based on the MergeStrategy</FONT></SPAN></SPAN></SPAN></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore">20.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;</SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri><FONT face=Calibri><FONT size=3>it makes a <I><U>clone</U></I> of "theEntity" and adds that <I>clone </I>to the EntityCache ... unless "theEntity" happens to already be in the cache in which case it is ignored ... which means that</FONT></FONT></FONT></FONT></P><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore"><FONT size=3 face=Calibri>21.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp; </SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri>using our example and assuming that&nbsp;"theEntity"&nbsp;was not already in the manager, the entity instance in the cache is not the same as the entity instance you imported, although their keys are equal; the following is true:</P><DIV><FONT size=2 face="Courier New, Courier, mono">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theEntity != theManager.FindEntity(theEntity.EntityAspect.EntityKey)</FONT></FONT></FONT></DIV><DIV><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore">22.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;</SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri><FONT face=Calibri><FONT size=3>A "clone"&nbsp;is a copy of an entity, equivalent to calling<FONT size=2 face="Courier New, Courier, mono"> ((ICloneable)theEntity).Clone();</FONT>&nbsp; </P><DIV><P style="TEXT-INDENT: -0.25in; MARGIN: 0in 0in 0pt 38.25pt; mso-list: l0 level1 lfo1" =MsoListParagraph><SPAN style="mso-fareast-font-family: Calibri"><SPAN style="mso-list: Ignore">23.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;</SPAN></SPAN></SPAN><FONT size=3><FONT face=Calibri><FONT face=Calibri><FONT size=3>This is a copy of&nbsp;the entity, not of its related entities.</P><DIV></DIV><DIV>&nbsp;</DIV><DIV>Enjoy!</DIV></FONT></FONT></FONT></FONT></FONT></FONT></FONT></FONT></DIV></DIV></FONT></FONT></o:p></FONT></FONT></DIV>]]>
   </description>
   <pubDate>Fri, 21 Aug 2009 15:15:16 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=1439&amp;PID=5227#5227</guid>
  </item> 
 </channel>
</rss>