<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LINQ &#8211; Oh my blog! Whee!</title>
	<atom:link href="https://whee.dk/tag/linq/feed/" rel="self" type="application/rss+xml" />
	<link>https://whee.dk</link>
	<description>Wrong by design</description>
	<lastBuildDate>Wed, 09 Jul 2008 17:24:47 +0000</lastBuildDate>
	<language>da-DK</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.1</generator>
	<item>
		<title>LINQ code side cascading deletes</title>
		<link>https://whee.dk/2008/06/5/</link>
					<comments>https://whee.dk/2008/06/5/#respond</comments>
		
		<dc:creator><![CDATA[Arne Sostack]]></dc:creator>
		<pubDate>Wed, 18 Jun 2008 10:45:42 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[cascade delete]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[VB.Net]]></category>
		<guid isPermaLink="false">http://whee.dk/?p=5</guid>

					<description><![CDATA[Over the last week or so, I&#8217;ve been searching around the net for a solution to a problem at work. The problem being that I have a bunch of LINQed objects with parent-child relations between them &#8211; the classic Orders -&#62; OrderLines problem. Normally, you&#8217;d create a relation with CASCADE DELETE set on it, but [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Over the last week or so, I&#8217;ve been searching around the net for a solution to a problem at work. The problem being that I have a bunch of LINQed objects with parent-child relations between them &#8211; the classic Orders -&gt; OrderLines problem.</p>
<p>Normally, you&#8217;d create a relation with CASCADE DELETE set on it, but for these objects, I had to do more than just delete the relevant records &#8211; for example, I could have a file on disk related to each OrderLine. Deleting the OrderLine should delete the file as well &#8211; even if it was a cascaded delete.</p>
<p>You could implement the partial methods DeleteOrder and DeleteOrderLine on the Context class, and delete the file from there. That works for direct deletes, but not for the cascade case, since that only takes place on the sql-server. Also, you cannot call DeleteOnSubmit during SubmitChanges, which is where the partial methods are called.</p>
<p>All I got from google was a bunch of notes on using <a href="http://blogs.msdn.com/bethmassi/archive/2007/10/02/linq-to-sql-and-one-to-many-relationships.aspx">DeleteOnNull</a>, which doesn&#8217;t help me any.</p>
<p>Finally, it dawned on me: You can override SubmitChanges on the Context class. Here&#8217;s how I did it:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode"><span class="kwrd">Public</span> <span class="kwrd">Overrides</span> <span class="kwrd">Sub</span> SubmitChanges(<span class="kwrd">ByVal</span> failureMode <span class="kwrd">As</span> System.Data.Linq.ConflictMode)
  <span class="kwrd">Dim</span> c <span class="kwrd">As</span> <span class="kwrd">Integer</span> = 0
    <span class="kwrd">Dim</span> cs <span class="kwrd">As</span> Changeset = <span class="kwrd">Me</span>.GetChangeSet()

    <span class="rem">' Use a While loop because the count can (will) increase</span>
    <span class="kwrd">While</span> c &gt; cs.Deletes.Count
        <span class="kwrd">Dim</span> deleted <span class="kwrd">As</span> IDeleteEventHandler = <span class="kwrd">TryCast</span>(cs.Deleted(c), IDeleteEventHandler)
        <span class="kwrd">If</span> deleted IsNot <span class="kwrd">Nothing</span> <span class="kwrd">Then</span>
            <span class="rem">' Notify the object that it is about to be deleted.</span>
            deleted.OnDeleting(<span class="kwrd">Me</span>)
        <span class="kwrd">End</span> <span class="kwrd">If</span>
    <span class="kwrd">End</span> <span class="kwrd">While</span>

    <span class="kwrd">MyBase</span>.SubmitChanges(failureMode)

    <span class="rem">' Here I can use a For Each loop because it's</span>
    <span class="rem">' too late to add to the collection anyhow.</span>
    <span class="kwrd">For</span> <span class="kwrd">Each</span> deleted <span class="kwrd">As</span> IDeleteEventHandler <span class="kwrd">In</span> cs.Deleted
        <span class="rem">' Notify the object that it has been deleted.</span>
        deleted.OnDeleted()
    <span class="kwrd">Next</span>
<span class="kwrd">End</span> <span class="kwrd">Sub</span>
</pre>
<p>And on each object that had to cascade deletes code side, I&#8217;d implement the IDeleteEventHandler interface (that I defined to have two methods &#8211; OnDelete and OnDeleted), like so:</p>
<pre class="csharpcode"><span class="kwrd">Partial</span> <span class="kwrd">Public</span> <span class="kwrd">Class</span> Order
    <span class="kwrd">Implements</span> IDeleteEventHandler

    <span class="kwrd">Public</span> <span class="kwrd">Sub</span> OnDeleting(<span class="kwrd">ByVal</span> context <span class="kwrd">As</span> Context) <span class="kwrd">Implements</span> IDeleteEventHandler.OnDeleting
        <span class="rem">' Also delete OrderLines</span>
        context.OrderLines.DeleteAllOnSubmit(<span class="kwrd">Me</span>.OrderLines)
    <span class="kwrd">End</span> <span class="kwrd">Sub</span> 

    <span class="kwrd">Public</span> <span class="kwrd">Sub</span> OnDeleted() <span class="kwrd">Implements</span> IDeleteEventHandler.OnDeleted
        <span class="rem">' No context passed to this method because you shouldn't</span>
        <span class="rem">' do stuff with the context here.</span>

        <span class="rem">' Delete order file from disk</span>
        System.IO.File.Delete(<span class="kwrd">Me</span>.FileName)
    <span class="kwrd">End</span> <span class="kwrd">Sub</span>
<span class="kwrd">End</span> <span class="kwrd">Class</span>
</pre>
<p>Tah daaaah! There you have it, code side cascading deletes in LINQ.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://whee.dk/2008/06/5/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Object Caching 29/40 objects using Disk
Page Caching using Disk: Enhanced 

Served from: whee.dk @ 2026-07-13 23:57:59 by W3 Total Cache
-->