<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Detached Data in DLinq</title>
	<atom:link href="http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/feed/" rel="self" type="application/rss+xml" />
	<link>http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/</link>
	<description>Simplicy in Software Design and Code</description>
	<lastBuildDate>Mon, 09 Nov 2009 09:10:16 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Maxi</title>
		<link>http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-19</link>
		<dc:creator>Maxi</dc:creator>
		<pubDate>Mon, 09 Nov 2009 09:10:16 +0000</pubDate>
		<guid isPermaLink="false">http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-19</guid>
		<description>I tried this code and it works fine most of the time.
only when I have two entities which share the same PK and have a one to one relationship.
The PK will be lose after the clone.
Does anyone encounter this problem? any workaround?</description>
		<content:encoded><![CDATA[<p>I tried this code and it works fine most of the time.<br />
only when I have two entities which share the same PK and have a one to one relationship.<br />
The PK will be lose after the clone.<br />
Does anyone encounter this problem? any workaround?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul Young</title>
		<link>http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-15</link>
		<dc:creator>Paul Young</dc:creator>
		<pubDate>Wed, 30 Apr 2008 21:08:08 +0000</pubDate>
		<guid isPermaLink="false">http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-15</guid>
		<description>Chris,
I implemented your detachableEntity class and I am trying to use an ObjectDataSource in an ASP.NET webpage to bind and update the entity class.  For some reason, the Original property that the inheritance is adding to the entity is not being serialized (or at least it is null when the postback fires the update method).  I have verified that it is set when the form loads but for some reason it is being cleared out.  So when I call attach method as follows...

context.Customers.Attach(instance, instance.Original);

...it throws an exception stating that &quot;Value cannot be null.  Parameter name: original.&quot;

Any clue?  Have you used your code in a similar fashion (i.e. GridView, DetailsView, etc.)?</description>
		<content:encoded><![CDATA[<p>Chris,<br />
I implemented your detachableEntity class and I am trying to use an ObjectDataSource in an ASP.NET webpage to bind and update the entity class.  For some reason, the Original property that the inheritance is adding to the entity is not being serialized (or at least it is null when the postback fires the update method).  I have verified that it is set when the form loads but for some reason it is being cleared out.  So when I call attach method as follows&#8230;</p>
<p>context.Customers.Attach(instance, instance.Original);</p>
<p>&#8230;it throws an exception stating that &#8220;Value cannot be null.  Parameter name: original.&#8221;</p>
<p>Any clue?  Have you used your code in a similar fashion (i.e. GridView, DetailsView, etc.)?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bjorn Bailleul</title>
		<link>http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-14</link>
		<dc:creator>Bjorn Bailleul</dc:creator>
		<pubDate>Mon, 28 Apr 2008 13:51:59 +0000</pubDate>
		<guid isPermaLink="false">http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-14</guid>
		<description>&lt;b&gt;Warning:&lt;/b&gt; This code won&#039;t work when you have read only properties in your class. It will try to write the property and fail to do so because it has not setter for the property.

Add the following line to you code in the &lt;i&gt;detachableEntity&lt;/i&gt; - &lt;i&gt;public static T Clone(T item)&lt;/i&gt; method to avoid this:
&lt;code&gt;
         // copy all subclass properties.
         foreach (PropertyInfo prop in item.GetType().GetProperties())
         {
            PropertyInfo prop2 = item.GetType().GetProperty(prop.Name);

           // Check if the property can be written
           if(prop2.CanWrite)
             prop2.SetValue(newItem, prop.GetValue(item, null), null);
         }
&lt;/code</description>
		<content:encoded><![CDATA[<p><b>Warning:</b> This code won&#8217;t work when you have read only properties in your class. It will try to write the property and fail to do so because it has not setter for the property.</p>
<p>Add the following line to you code in the <i>detachableEntity</i> &#8211; <i>public static T Clone(T item)</i> method to avoid this:<br />
<code><br />
         // copy all subclass properties.<br />
         foreach (PropertyInfo prop in item.GetType().GetProperties())<br />
         {<br />
            PropertyInfo prop2 = item.GetType().GetProperty(prop.Name);</p>
<p>           // Check if the property can be written<br />
           if(prop2.CanWrite)<br />
             prop2.SetValue(newItem, prop.GetValue(item, null), null);<br />
         }<br />
&lt;/code</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stijn</title>
		<link>http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-13</link>
		<dc:creator>Stijn</dc:creator>
		<pubDate>Wed, 23 Apr 2008 07:32:46 +0000</pubDate>
		<guid isPermaLink="false">http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-13</guid>
		<description>Hi, thx for code,

There is one problem. The reflection code doesnt work on child relations.
I have a customer and i have orders of that customer.
I load customer with deferredloadingEnabled = false AND dataloadOptions to load the orders of that customers
Dim options As DataLoadOptions = New DataLoadOptions
options.LoadWith(Of customer)(Function(x As customer) x.orders)

When i use linq2sql without detachableEntity, i load 1 customer and 3 orders of that customer.

With detachableEntity, cust1.orders.count = 0 (i expect 3) and the cust1.original.count = 3
The problem is on this line when the property is an entityset.
            prop2.SetValue(newItem, prop.GetValue(item, null), null);


So in that situation my original orders are at cust1.original.orders.original and my current orders are at cust.original.orders.

A little strange, i think

How can i fix this issue?</description>
		<content:encoded><![CDATA[<p>Hi, thx for code,</p>
<p>There is one problem. The reflection code doesnt work on child relations.<br />
I have a customer and i have orders of that customer.<br />
I load customer with deferredloadingEnabled = false AND dataloadOptions to load the orders of that customers<br />
Dim options As DataLoadOptions = New DataLoadOptions<br />
options.LoadWith(Of customer)(Function(x As customer) x.orders)</p>
<p>When i use linq2sql without detachableEntity, i load 1 customer and 3 orders of that customer.</p>
<p>With detachableEntity, cust1.orders.count = 0 (i expect 3) and the cust1.original.count = 3<br />
The problem is on this line when the property is an entityset.<br />
            prop2.SetValue(newItem, prop.GetValue(item, null), null);</p>
<p>So in that situation my original orders are at cust1.original.orders.original and my current orders are at cust.original.orders.</p>
<p>A little strange, i think</p>
<p>How can i fix this issue?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Condron</title>
		<link>http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-12</link>
		<dc:creator>Chris Condron</dc:creator>
		<pubDate>Thu, 17 Apr 2008 01:38:15 +0000</pubDate>
		<guid isPermaLink="false">http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-12</guid>
		<description>Hi Ian,
 I have tried a few things and cannot reproduce your problem. Thinking about it I think I know what might be happening and how to check.
 In the onLoad function we do three things 
   1) create a new instance of the type
   2) iterate through every property and copy it to the new type
   3) call this recursivly until this.original == null

So either, 
Creating a copy of the item is causing something in your class structure to add the new item to insert list
or
Copying every property we find is adding a new item to the instert list

I don&#039;t think it is the recursion because your are only getting one extra per call.

How to check:

1) make DataContext public static (just for the test)
2) set break points inside the clone function.
3) evaluate [your class].DataContext.GetChangeSet() after each step

This will tell you what step is creating the insert record.
You can then find out the side efect code that is being invoked to add the insert.


Let me know what you find. 

  Thanks
    Chris</description>
		<content:encoded><![CDATA[<p>Hi Ian,<br />
 I have tried a few things and cannot reproduce your problem. Thinking about it I think I know what might be happening and how to check.<br />
 In the onLoad function we do three things<br />
   1) create a new instance of the type<br />
   2) iterate through every property and copy it to the new type<br />
   3) call this recursivly until this.original == null</p>
<p>So either,<br />
Creating a copy of the item is causing something in your class structure to add the new item to insert list<br />
or<br />
Copying every property we find is adding a new item to the instert list</p>
<p>I don&#8217;t think it is the recursion because your are only getting one extra per call.</p>
<p>How to check:</p>
<p>1) make DataContext public static (just for the test)<br />
2) set break points inside the clone function.<br />
3) evaluate [your class].DataContext.GetChangeSet() after each step</p>
<p>This will tell you what step is creating the insert record.<br />
You can then find out the side efect code that is being invoked to add the insert.</p>
<p>Let me know what you find. </p>
<p>  Thanks<br />
    Chris</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Condron</title>
		<link>http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-11</link>
		<dc:creator>Chris Condron</dc:creator>
		<pubDate>Mon, 14 Apr 2008 15:48:12 +0000</pubDate>
		<guid isPermaLink="false">http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-11</guid>
		<description>Hmmm. I&#039;ll take a look.
-Chris</description>
		<content:encoded><![CDATA[<p>Hmmm. I&#8217;ll take a look.<br />
-Chris</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ian</title>
		<link>http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-10</link>
		<dc:creator>Ian</dc:creator>
		<pubDate>Mon, 14 Apr 2008 08:11:27 +0000</pubDate>
		<guid isPermaLink="false">http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-10</guid>
		<description>Hi,

I&#039;ve used your DetachableEntity class as laid out here, and all works brilliantly during the update, however, there seems to be an issue when I try to delete an entity.  I&#039;m not sure if I&#039;m doing something wrong, so I wanted to check if you had experienced any problems during deletes.

All I have done is inherit one of my entity classes from DetachableEntity in a partial class, and provided an implementation of the OnLoaded partial method as described in your article.

If I then call DeleteAllOnSubmit passing in an Expression&lt;Func&gt; predicate to match some criteria I don&#039;t get any delete SQL hit my database.  If I inspect the return value of the DataContext.GetChangeSet() I can see the same number of items in the Insert collection that are in the Delete section, and these items appear to be same.  I guess the context is normalising this and determining that nothing needs to be deleted.

If I remove the call to base.OnDataLoaded() in the partial class (effectively removing the assignment of the cloned entity), I can run through the same code but this time the GetChangeSet result doesn&#039;t have anything the Inserts collection, and has the correct number in the Deletes section, so the rows are correctly deleted.  But of course, the update method no longer works.

My delete method is on a generic base class (and I realise that it will select the ones to delete before actually deleting them), but looks like this:

public virtual void Delete(Expression&lt;Func&gt; predicate)
{
    if (predicate == null)
    {
        throw new ArgumentNullException(&quot;predicate&quot;);
    }

    /// TODO: See if there&#039;s a way we can delete items without having to 
    /// perform a SELECT first.
    /// 
    var query = DataContext.GetTable().Where(predicate);
    DataContext.GetTable().DeleteAllOnSubmit(query);
    DataContext.SubmitChanges();
} 

I then call the method with something like Delete(t =&gt; t.SomeProperty == &quot;foo&quot;);

Has anyone used this code and experienced anything similar?

Thanks
Ian</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I&#8217;ve used your DetachableEntity class as laid out here, and all works brilliantly during the update, however, there seems to be an issue when I try to delete an entity.  I&#8217;m not sure if I&#8217;m doing something wrong, so I wanted to check if you had experienced any problems during deletes.</p>
<p>All I have done is inherit one of my entity classes from DetachableEntity in a partial class, and provided an implementation of the OnLoaded partial method as described in your article.</p>
<p>If I then call DeleteAllOnSubmit passing in an Expression&lt;Func&gt; predicate to match some criteria I don&#8217;t get any delete SQL hit my database.  If I inspect the return value of the DataContext.GetChangeSet() I can see the same number of items in the Insert collection that are in the Delete section, and these items appear to be same.  I guess the context is normalising this and determining that nothing needs to be deleted.</p>
<p>If I remove the call to base.OnDataLoaded() in the partial class (effectively removing the assignment of the cloned entity), I can run through the same code but this time the GetChangeSet result doesn&#8217;t have anything the Inserts collection, and has the correct number in the Deletes section, so the rows are correctly deleted.  But of course, the update method no longer works.</p>
<p>My delete method is on a generic base class (and I realise that it will select the ones to delete before actually deleting them), but looks like this:</p>
<p>public virtual void Delete(Expression&lt;Func&gt; predicate)<br />
{<br />
    if (predicate == null)<br />
    {<br />
        throw new ArgumentNullException(&#8220;predicate&#8221;);<br />
    }</p>
<p>    /// TODO: See if there&#8217;s a way we can delete items without having to<br />
    /// perform a SELECT first.<br />
    ///<br />
    var query = DataContext.GetTable().Where(predicate);<br />
    DataContext.GetTable().DeleteAllOnSubmit(query);<br />
    DataContext.SubmitChanges();<br />
} </p>
<p>I then call the method with something like Delete(t =&gt; t.SomeProperty == &#8220;foo&#8221;);</p>
<p>Has anyone used this code and experienced anything similar?</p>
<p>Thanks<br />
Ian</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tumay</title>
		<link>http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-9</link>
		<dc:creator>tumay</dc:creator>
		<pubDate>Tue, 01 Apr 2008 19:44:30 +0000</pubDate>
		<guid isPermaLink="false">http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-9</guid>
		<description>List&lt;Answer&gt;</description>
		<content:encoded><![CDATA[<p>List&lt;Answer&gt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tumay</title>
		<link>http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-8</link>
		<dc:creator>tumay</dc:creator>
		<pubDate>Tue, 01 Apr 2008 19:43:08 +0000</pubDate>
		<guid isPermaLink="false">http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-8</guid>
		<description>Hi Chris,

How do you use this class to update a collection of updating objects like List?

tumay</description>
		<content:encoded><![CDATA[<p>Hi Chris,</p>
<p>How do you use this class to update a collection of updating objects like List?</p>
<p>tumay</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Condron</title>
		<link>http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-7</link>
		<dc:creator>Chris Condron</dc:creator>
		<pubDate>Sat, 08 Mar 2008 01:55:44 +0000</pubDate>
		<guid isPermaLink="false">http://condron.wordpress.com/2008/02/24/detached-data-in-dlinq/#comment-7</guid>
		<description>Hi Alexander,
  I tried to replicate your error, and I could not.
Here is the code I used to try to replicate it.

&lt;code&gt;
static void Main(string[] args)
{
&#160;&#160;//Create new Entity
&#160;&#160;Address home = new Address();
&#160;&#160;	//Create new data context
&#160;&#160;	AdventureWorksDataContext AW = new AdventureWorksDataContext();
&#160;&#160;	//Get Data
&#160;&#160;	home = AW.Addresses.First();
&#160;&#160;
&#160;&#160;	// Disconnect Data Context
&#160;&#160;	AW = null;
&#160;&#160;
&#160;&#160;	//Modify Data
&#160;&#160;	home.City = home.City + &quot;AAA&quot;;
&#160;&#160;
&#160;&#160;	// New Data context
&#160;&#160;	AW = new AdventureWorksDataContext();
&#160;&#160;	//Attach Entity
&#160;&#160;	AW.Addresses.Attach(home, home.original);
&#160;&#160;	//Review Changes
&#160;&#160;	ChangeSet Changes = AW.GetChangeSet();
&#160;&#160;
&#160;&#160;	//Update Data Source
&#160;&#160;	AW.SubmitChanges();
&#160;&#160;
&#160;&#160;	//Dispose Data Context
&#160;&#160;	AW.Dispose();
&#160;&#160;	AW = null;
&#160;&#160;	//Dispose Entity
&#160;&#160;	home = null;
&#160;&#160;
&#160;&#160;	//Create new Data Context
&#160;&#160;	using (AW = new AdventureWorksDataContext())
&#160;&#160;	{
&#160;&#160;&#160;&#160;		//Create new Entity
&#160;&#160;&#160;&#160;		Address NewHome = AW.Addresses.FirstOrDefault(addr =&gt; addr.City.Contains(&quot;AAA&quot;));
&#160;&#160;&#160;&#160;		//Modify Data (remove earlier changes)
&#160;&#160;&#160;&#160;		NewHome.City = NewHome.City.TrimEnd(new char[] { &#039;A&#039; });
&#160;&#160;&#160;&#160;		//Submit Changes 
&#160;&#160;&#160;&#160;		AW.SubmitChanges();
&#160;&#160;	}
}

&lt;/code&gt;
I am using vs2008 and sql server 2005
I updated the usage example with this.</description>
		<content:encoded><![CDATA[<p>Hi Alexander,<br />
  I tried to replicate your error, and I could not.<br />
Here is the code I used to try to replicate it.</p>
<p><code><br />
static void Main(string[] args)<br />
{<br />
&nbsp;&nbsp;//Create new Entity<br />
&nbsp;&nbsp;Address home = new Address();<br />
&nbsp;&nbsp;	//Create new data context<br />
&nbsp;&nbsp;	AdventureWorksDataContext AW = new AdventureWorksDataContext();<br />
&nbsp;&nbsp;	//Get Data<br />
&nbsp;&nbsp;	home = AW.Addresses.First();<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;	// Disconnect Data Context<br />
&nbsp;&nbsp;	AW = null;<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;	//Modify Data<br />
&nbsp;&nbsp;	home.City = home.City + "AAA";<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;	// New Data context<br />
&nbsp;&nbsp;	AW = new AdventureWorksDataContext();<br />
&nbsp;&nbsp;	//Attach Entity<br />
&nbsp;&nbsp;	AW.Addresses.Attach(home, home.original);<br />
&nbsp;&nbsp;	//Review Changes<br />
&nbsp;&nbsp;	ChangeSet Changes = AW.GetChangeSet();<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;	//Update Data Source<br />
&nbsp;&nbsp;	AW.SubmitChanges();<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;	//Dispose Data Context<br />
&nbsp;&nbsp;	AW.Dispose();<br />
&nbsp;&nbsp;	AW = null;<br />
&nbsp;&nbsp;	//Dispose Entity<br />
&nbsp;&nbsp;	home = null;<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;	//Create new Data Context<br />
&nbsp;&nbsp;	using (AW = new AdventureWorksDataContext())<br />
&nbsp;&nbsp;	{<br />
&nbsp;&nbsp;&nbsp;&nbsp;		//Create new Entity<br />
&nbsp;&nbsp;&nbsp;&nbsp;		Address NewHome = AW.Addresses.FirstOrDefault(addr =&gt; addr.City.Contains("AAA"));<br />
&nbsp;&nbsp;&nbsp;&nbsp;		//Modify Data (remove earlier changes)<br />
&nbsp;&nbsp;&nbsp;&nbsp;		NewHome.City = NewHome.City.TrimEnd(new char[] { 'A' });<br />
&nbsp;&nbsp;&nbsp;&nbsp;		//Submit Changes<br />
&nbsp;&nbsp;&nbsp;&nbsp;		AW.SubmitChanges();<br />
&nbsp;&nbsp;	}<br />
}</p>
<p></code><br />
I am using vs2008 and sql server 2005<br />
I updated the usage example with this.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
