<?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 : Cascading Delete/Modification</title>
  <link>http://www.ideablade.com/forum/</link>
  <description>This is an XML content feed of; DevForce Community Forum : DevForce 2009 : Cascading Delete/Modification</description>
  <pubDate>Sat, 11 Apr 2026 05:50:27 -700</pubDate>
  <lastBuildDate>Fri, 27 Mar 2009 20:05:21 -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=1168</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>Cascading Delete/Modification : You&amp;#039;re right that use of...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=1168&amp;PID=4207#4207</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=11" rel="nofollow">kimj</a><br /><strong>Subject:</strong> 1168<br /><strong>Posted:</strong> 27-Mar-2009 at 8:05pm<br /><br />You're right that use of the EntityChanging event to handle cascaded deletions falls short when asynchronous navigation is involved.&nbsp; One workaround, when possible,&nbsp;is to pre-load the dependent objects so you avoid this situation altogether.&nbsp; When that's not possible,&nbsp;we really don't have a great answer for this&nbsp;right now. <DIV>&nbsp;</DIV><DIV>I did look at using the AsyncSerialTask to do this and came up with something&nbsp;which works, but is a bit ugly.&nbsp; Pending entity resolution is not currently handled the same way as other asynchronous actions within DevForce, but we will look at making this easier.&nbsp; The sample below takes advantage of how the AsyncSerialTask passes arguments and results from step to step.&nbsp; </DIV><DIV>&nbsp;</DIV><DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AsyncSerialTask.Create&lt;Customer&gt;("DeleteCustomer")<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .AddAsyncAction&lt;AsyncEventArgs&gt;(SetupDeletion)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .AddAction(args =&gt; DoDelete((Customer)args.UserState))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .Execute(aCustomerToBeDeleted);<BR></DIV><DIV>&nbsp;</DIV><DIV>&nbsp;&nbsp;&nbsp; // "callback" is called when entities are available</DIV><DIV>&nbsp;&nbsp;&nbsp; //&nbsp; the Customer is passed as the userState to the AsyncEventArgs</DIV><DIV>&nbsp;&nbsp;&nbsp; private void SetupDeletion(Customer cust, AsyncCompletedCallback&lt;AsyncEventArgs&gt; callback) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var list = cust.OrderSummaries;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bool isPending = list.IsPendingEntityList;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (isPending) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; list.PendingEntityListResolved += (o, args) =&gt; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; callback(new AsyncEventArgs(cust));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; callback(new AsyncEventArgs(cust));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }</DIV><DIV>&nbsp;</DIV><DIV>&nbsp;&nbsp;&nbsp; // Called by AsyncSerialTask when the prior step completes.</DIV><DIV>&nbsp;&nbsp;&nbsp; private void DoDelete(Customer cust) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cust.EntityAspect.Delete();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach (var order in cust.OrderSummaries.ToList()) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; order.EntityAspect.Delete();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }<BR></DIV>]]>
   </description>
   <pubDate>Fri, 27 Mar 2009 20:05:21 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=1168&amp;PID=4207#4207</guid>
  </item> 
  <item>
   <title>Cascading Delete/Modification : I&amp;#039;ve got a followup question...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=1168&amp;PID=4204#4204</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=452" rel="nofollow">ken.nelson</a><br /><strong>Subject:</strong> 1168<br /><strong>Posted:</strong> 27-Mar-2009 at 1:25pm<br /><br /><P>I've got a followup question on this one, specifically for Silverlight.</P><DIV>The above solution appears to work properly, when the Orders are in the cache.&nbsp; However when not in the cache, I've got to do the pending list solution:</DIV><DIV><FONT face="Courier New, Courier, mono"></FONT>&nbsp;</DIV><DIV><FONT face="Courier New, Courier, mono">public static void EntityChanging(object sender, IdeaBlade.EntityModel.v4.EntityChangingEventArgs</FONT><FONT face="Courier New, Courier, mono"> e)<BR></FONT><FONT face="Courier New, Courier, mono">{<BR>&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT face="Courier New, Courier, mono">if (e.Action == IdeaBlade.EntityModel.v4.EntityAction</FONT><FONT face="Courier New, Courier, mono">.Delete)</FONT></DIV><DIV><FONT face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp; {</FONT></DIV><DIV><FONT face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT face="Courier New, Courier, mono">if (e.Entity is Customer</FONT><FONT face="Courier New, Courier, mono">)</FONT></DIV><DIV><FONT face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</FONT></DIV><DIV><FONT face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Customer customer = e.Entity as Customer;</FONT></DIV><DIV><FONT face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT face="Courier New, Courier, mono"><FONT face="Courier New, Courier, mono">if (customer.Orders.IsPendingEntityList)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT face="Courier New">{</FONT><DIV><FONT face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; customer.Orders.PendingEntityListResolved += </FONT></DIV><DIV><FONT face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new&nbsp;EventHandler&lt;PendingEntityListResolvedEventArgs&lt;Order&gt;&gt;(OrdersResolvedForDelete);</FONT></DIV><DIV><FONT face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR></FONT><FONT face="Courier New, Courier, mono">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else</FONT></DIV><DIV><FONT face="Courier New, Courier, mono">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR></FONT><FONT face="Courier New, Courier, mono">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; foreach (var order in customer.Orders.ToList())</FONT></DIV><DIV><FONT face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; order.EntityAspect.Delete();</FONT></DIV><DIV><FONT face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT></DIV><DIV></FONT><FONT face="Courier New, Courier, mono">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT face="Courier New, Courier, mono">}<BR>&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT face="Courier New, Courier, mono">}<BR></FONT><FONT face="Courier New, Courier, mono">}</FONT></DIV></DIV><DIV>&nbsp;</DIV><DIV><FONT face="Courier New, Courier, mono">private void OrdersResolvedForDelete(object sender, PendingEntityListResolvedEventArgs&lt;Order&gt; e)</FONT></DIV><DIV><FONT face="Courier New, Courier, mono">{</FONT></DIV><DIV><FONT face="Courier New, Courier, mono">&nbsp;&nbsp;&nbsp;&nbsp; foreach (var order in e.ResolvedEntities)</FONT><DIV><FONT face="Courier New, Courier, mono">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; order.EntityAspect.Delete();</FONT></DIV></DIV><DIV><FONT face="Courier New, Courier, mono">}</FONT></DIV><DIV><FONT face="Courier New"></FONT>&nbsp;</DIV><DIV><FONT face="Courier New"></FONT><FONT color=#dfdfbf><FONT color=#000000>The problem is, this&nbsp;doesn't appear to work properly when the list is pending.&nbsp; It seems that if the Customer is marked as Deleted before the pending EntityList is resolved, then the e.ResolvedEntities will come back empty and the related Orders will never get deleted, which ultimately results in a foreign key&nbsp;exception being thrown.</FONT></FONT></DIV><DIV><FONT color=#dfdfbf><FONT color=#000000></FONT></FONT>&nbsp;</DIV><DIV><FONT color=#dfdfbf><FONT color=#000000>Any idea's how to get around this?</FONT></FONT></DIV><DIV><FONT color=#dfdfbf><FONT color=#000000></FONT></FONT>&nbsp;</DIV><DIV><FONT color=#dfdfbf><FONT color=#000000>Thanks,</FONT></FONT></DIV><DIV><FONT color=#dfdfbf><FONT color=#000000>Ken</FONT></DIV></FONT>]]>
   </description>
   <pubDate>Fri, 27 Mar 2009 13:25:11 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=1168&amp;PID=4204#4204</guid>
  </item> 
  <item>
   <title>Cascading Delete/Modification : Cool, that worked, thanks! </title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=1168&amp;PID=4175#4175</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=452" rel="nofollow">ken.nelson</a><br /><strong>Subject:</strong> 1168<br /><strong>Posted:</strong> 25-Mar-2009 at 8:39am<br /><br />Cool, that worked, thanks!]]>
   </description>
   <pubDate>Wed, 25 Mar 2009 08:39:48 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=1168&amp;PID=4175#4175</guid>
  </item> 
  <item>
   <title>Cascading Delete/Modification : The problem is that the EntityGroup...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=1168&amp;PID=4172#4172</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=11" rel="nofollow">kimj</a><br /><strong>Subject:</strong> 1168<br /><strong>Posted:</strong> 24-Mar-2009 at 4:33pm<br /><br />The problem is that the EntityGroup on which the handler is placed is not the "real" EntityGroup holding the entity. Confusing, yes.&nbsp; <DIV>&nbsp;</DIV><DIV>Setting the handler in the entity constructor is not a good idea for two reasons - 1) the event is fired for the EntityGroup and creating a handler for each entity instance means there will be many more handlers than needed, and 2) more importantly, and for obscure reasons, the EntityGroup used in entity construction turns out not to be the EntityGroup used to cache the entities.&nbsp; What you should do instead is ask the EntityManager for the EntityGroup and set the handler once:</DIV><DIV>&nbsp;</DIV><DIV>&nbsp;&nbsp;&nbsp;&nbsp; _entityManager.GetEntityGroup(typeof(Customer)).EntityChanging += EntityGroup_EntityChanging;</DIV><DIV>&nbsp;</DIV><DIV>The handler can get the Entity involved via the EntityChangingEventArgs.</DIV><DIV>&nbsp;</DIV><DIV>We'll take a look at how we can better document the EntityGroup and its events, or change the API, so this isn't so cryptic.</DIV>]]>
   </description>
   <pubDate>Tue, 24 Mar 2009 16:33:00 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=1168&amp;PID=4172#4172</guid>
  </item> 
  <item>
   <title>Cascading Delete/Modification : We&amp;#039;re having a problem while...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=1168&amp;PID=4163#4163</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=452" rel="nofollow">ken.nelson</a><br /><strong>Subject:</strong> 1168<br /><strong>Posted:</strong> 24-Mar-2009 at 10:24am<br /><br /><DIV>We're having a problem while deleting an entity that has related entities.&nbsp; In DevForce Classic, you used to be able to override Delete() and handle any additional cleanup there, but that seems to have been removed from EF.&nbsp; The Reference Help document indicates that to get around this:</DIV><DIV>&nbsp;</DIV><DIV><table width="99%"><tr><td class="BBquote"></DIV><DIV><P>The <a href="ms-its:C:%5CProgram%20Files%5CIdeaBlade%20DevForce%20EF%5C&#068;ocumentati&#111;n%5CIdeaBladeDevForceV4HelpReference.chm::/IdeaBlade.EntityModel.v4~IdeaBlade.EntityModel.v4.EntityGroup~EntityChanging_EV.html" target="_blank"><U>IdeaBlade.EntityModel.v4.EntityGroup.EntityChanging</U></A> and <a href="ms-its:C:%5CProgram%20Files%5CIdeaBlade%20DevForce%20EF%5C&#068;ocumentati&#111;n%5CIdeaBladeDevForceV4HelpReference.chm::/IdeaBlade.EntityModel.v4~IdeaBlade.EntityModel.v4.EntityGroup~EntityChanged_EV.html" target="_blank"><U>IdeaBlade.EntityModel.v4.EntityGroup.EntityChanged</U></A> events will fire during a <B>Delete</B> call with an EntityAction of <a href="ms-its:C:%5CProgram%20Files%5CIdeaBlade%20DevForce%20EF%5C&#068;ocumentati&#111;n%5CIdeaBladeDevForceV4HelpReference.chm::/IdeaBlade.EntityModel.v4~IdeaBlade.EntityModel.v4.EntityActi&#111;n~Delete.html" target="_blank"><U>IdeaBlade.EntityModel.v4.EntityAction.Delete</U></A>. </P></DIV><DIV></td></tr></table></DIV><DIV>&nbsp;</DIV><DIV>For another bad example... Customer has Orders, we want to remove the Orders associated with a Customer via code (meaning, we're not looking for a database solution here).&nbsp; Following the above guidelines, we have the following:</DIV><DIV><FONT color=#000000></FONT>&nbsp;</DIV><DIV><FONT face="Courier New, Courier, mono" color=#000000>public Customer()</FONT></DIV><DIV><FONT color=#000000><FONT face="Courier New, Courier, mono">{</FONT></DIV><DIV><DIV></FONT><FONT face="Courier New, Courier, mono" color=#000000>&nbsp;&nbsp;&nbsp;&nbsp; this.EntityAspect.EntityGroup.EntityChanging += new </FONT></DIV><DIV><FONT face="Courier New, Courier, mono" color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EventHandler&lt;IdeaBlade.EntityModel.v4.EntityChangingEventArgs&gt;(EntityGroup_EntityChanging);<BR></FONT><FONT face="Courier New, Courier, mono" color=#000000>}<BR></FONT></DIV><DIV><FONT face="Courier New, Courier, mono" color=#000000>private void EntityGroup_EntityChanging(object sender, IdeaBlade.EntityModel.v4.EntityChangingEventArgs e)<BR></FONT><FONT face="Courier New, Courier, mono" color=#000000>{<BR>&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT face="Courier New, Courier, mono" color=#000000>if (e.Action == IdeaBlade.EntityModel.v4.EntityAction.Delete)<BR>&nbsp;&nbsp;&nbsp;&nbsp; </FONT><FONT face="Courier New, Courier, mono"><FONT color=#000000>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</FONT><FONT color=#000000>foreach&nbsp;(var order in this.Orders)</FONT></FONT></DIV><DIV><FONT face="Courier New, Courier, mono" color=#000000>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; order.Delete();</FONT><FONT color=#000000><BR><FONT face="Courier New, Courier, mono">&nbsp;&nbsp;&nbsp;&nbsp; </FONT></FONT><FONT face="Courier New, Courier, mono"><FONT color=#000000>}<BR></FONT><FONT color=#000000>}</FONT></FONT></DIV><DIV><FONT face="Courier New, Courier, mono"></FONT>&nbsp;</DIV><DIV>The problem is when we delete the Customer, the EntityChanging event never fires.&nbsp; When we SaveChanges(), an Exception is thrown, indicating that there are still Orders referencing the Customer, which makes sense given our cleanup code never fires.</DIV><DIV>&nbsp;</DIV><DIV>Are we doing something incorrectly here?&nbsp; Is there a better way to handle this?</DIV><DIV>&nbsp;</DIV><DIV>Thanks,</DIV><DIV>Ken</DIV><DIV><FONT face="Courier New"></FONT><FONT color=#dfdfbf></FONT>&nbsp;</DIV><DIV><FONT color=#dfdfbf>&nbsp;</DIV></DIV></FONT><span style="font-size:10px"><br /><br />Edited by ken.nelson - 24-Mar-2009 at 1:32pm</span>]]>
   </description>
   <pubDate>Tue, 24 Mar 2009 10:24:04 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=1168&amp;PID=4163#4163</guid>
  </item> 
 </channel>
</rss>