<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Tip 33 – How cascade delete really works in EF</title><link>http://blogs.msdn.com/b/alexj/archive/2009/08/19/tip-33-how-cascade-delete-really-works-in-ef.aspx</link><description>Imagine that in your database you have a cascade delete on an FK relationship. Something like this: Here the Delete Rule says that when a Category is deleted all the related Products should be deleted too. If you generate an EF model from this database</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: Tip 33 – How cascade delete really works in EF</title><link>http://blogs.msdn.com/b/alexj/archive/2009/08/19/tip-33-how-cascade-delete-really-works-in-ef.aspx#10339391</link><pubDate>Tue, 14 Aug 2012 09:08:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:10339391</guid><dc:creator>kiran</dc:creator><description>&lt;p&gt;Hi Alex,&lt;/p&gt;
&lt;p&gt;I couldn&amp;#39;t get you , how exactly you override SaveChanges and delete all the Dependents.Can you explain with little more detail.It would be helpful if provided a sample code or with an example.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=10339391" width="1" height="1"&gt;</description></item><item><title>re: Tip 33 – How cascade delete really works in EF</title><link>http://blogs.msdn.com/b/alexj/archive/2009/08/19/tip-33-how-cascade-delete-really-works-in-ef.aspx#9936157</link><pubDate>Sat, 12 Dec 2009 23:53:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9936157</guid><dc:creator>Alex D James</dc:creator><description>&lt;p&gt;Thomas,&lt;/p&gt;
&lt;p&gt;Well you could do this by overriding SaveChanges, enumerating all the objectStateEntries in the ObjectContext.ObjectStateManager, looking for those in the deleted state. &lt;/p&gt;
&lt;p&gt;When you find one of the right type, you could have a rule that loads all the dependents, then when you finally call through to base.SaveChanges() EF would delete *all* the dependents even if there isn't a cascade delete rule in the database.&lt;/p&gt;
&lt;p&gt;Alex&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9936157" width="1" height="1"&gt;</description></item><item><title>re: Tip 33 – How cascade delete really works in EF</title><link>http://blogs.msdn.com/b/alexj/archive/2009/08/19/tip-33-how-cascade-delete-really-works-in-ef.aspx#9936145</link><pubDate>Sat, 12 Dec 2009 21:27:27 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9936145</guid><dc:creator>Thomas</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I am used to a different ORM based on ActiveRecord where it is possible to ensure that additional cleanup is performed on delete if necessary.&lt;/p&gt;
&lt;p&gt;This obviously comes at a cost as you need to retrieve the related entity in order to carry out its ondelete cleanup.&lt;/p&gt;
&lt;p&gt;Lets say we have a Blog with Post that can have Attachments, and the actual files are stored on a file server with only a path in the database.&lt;/p&gt;
&lt;p&gt;Now I want to ensure that deleting a Blog or a Post will always cleanup the files in the file system too.&lt;/p&gt;
&lt;p&gt;How would you go about implementing that in EF?&lt;/p&gt;
&lt;p&gt;Thanks,&lt;/p&gt;
&lt;p&gt;Thomas&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9936145" width="1" height="1"&gt;</description></item><item><title>re: Tip 33 – How cascade delete really works in EF</title><link>http://blogs.msdn.com/b/alexj/archive/2009/08/19/tip-33-how-cascade-delete-really-works-in-ef.aspx#9929582</link><pubDate>Sat, 28 Nov 2009 06:18:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9929582</guid><dc:creator>Alex D James</dc:creator><description>&lt;p&gt;@Luke,&lt;/p&gt;
&lt;p&gt;Absolutely, you should have the a cascade delete rule in the database, because EF can't be relied upon to delete every entity. It will only delete entities it knows about (i.e. that are in memory).&lt;/p&gt;
&lt;p&gt;Alex&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9929582" width="1" height="1"&gt;</description></item><item><title>re: Tip 33 – How cascade delete really works in EF</title><link>http://blogs.msdn.com/b/alexj/archive/2009/08/19/tip-33-how-cascade-delete-really-works-in-ef.aspx#9929560</link><pubDate>Sat, 28 Nov 2009 03:42:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9929560</guid><dc:creator>Luke</dc:creator><description>&lt;p&gt;So just for clarification, since #2 is not recommended, are you saying that the #1 recommended approach is having a cascading delete set up on the physical database? &amp;nbsp;Thanks.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9929560" width="1" height="1"&gt;</description></item><item><title>re: Tip 33 – How cascade delete really works in EF</title><link>http://blogs.msdn.com/b/alexj/archive/2009/08/19/tip-33-how-cascade-delete-really-works-in-ef.aspx#9924841</link><pubDate>Wed, 18 Nov 2009 23:05:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9924841</guid><dc:creator>CodeWarrior</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;I have a question. &amp;nbsp;When I tried adding the OnDelete action I still get the following error.&lt;/p&gt;
&lt;p&gt;{&amp;quot;The DELETE statement conflicted with the REFERENCE constraint \&amp;quot;FK_Orders_Customers\&amp;quot;. The conflict occurred in database \&amp;quot;C:\\METADATAPROVIDERSAMPLE\\METADATAPROVIDERWEBSITE\\APP_DATA\\NORTHWND.MDF\&amp;quot;, table \&amp;quot;dbo.Orders\&amp;quot;, column 'CustomerID'.\r\nThe statement has been terminated.&amp;quot;}&lt;/p&gt;
&lt;p&gt;Here is my code:&lt;/p&gt;
&lt;p&gt;NORTHWNDEntities1 ne = new NORTHWNDEntities1(); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt; var originalEntity = (from entity in ne.Customers&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; where entity.CustomerID == &amp;quot;ANTON&amp;quot; select entity).FirstOrDefault();&lt;/p&gt;
&lt;p&gt; &amp;nbsp;ne.DeleteObject(originalEntity);&lt;/p&gt;
&lt;p&gt; &amp;nbsp;ne.SaveChanges(true);&lt;/p&gt;
&lt;p&gt;Here is the XML file contents:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;Association Name=&amp;quot;FK_Orders_Customers&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;End Role=&amp;quot;Customers&amp;quot; Type=&amp;quot;NORTHWNDModel.Store.Customers&amp;quot; Multiplicity=&amp;quot;0..1&amp;quot; &amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;OnDelete Action=&amp;quot;Cascade&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;/End&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;End Role=&amp;quot;Orders&amp;quot; Type=&amp;quot;NORTHWNDModel.Store.Orders&amp;quot; Multiplicity=&amp;quot;*&amp;quot; &amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;/End&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;ReferentialConstraint&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;Principal Role=&amp;quot;Customers&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;PropertyRef Name=&amp;quot;CustomerID&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;/Principal&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;Dependent Role=&amp;quot;Orders&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;PropertyRef Name=&amp;quot;CustomerID&amp;quot; /&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;/Dependent&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;/ReferentialConstraint&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;/Association&amp;gt;&lt;/p&gt;
&lt;p&gt;Oh, and I did make the changes in the database.&lt;/p&gt;
&lt;p&gt;Any help would be greatly appreciated!!!&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
&lt;p&gt;CodeWarrior&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9924841" width="1" height="1"&gt;</description></item></channel></rss>