<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.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>Linq to SQL: How to Attach object to a different data context</title><link>http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx</link><description>After upgrading to Visual Studio 2008 RTM, you will have trouble updating Linq to SQL Classes which are read from one data context and then updated into another data context. You will get this exception during update: System.NotSupportedException: An</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>re: Linq to SQL: How to Attach object to a different data context</title><link>http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx#1744398</link><pubDate>Wed, 09 Dec 2009 09:45:27 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1744398</guid><dc:creator>Abhai Sharma</dc:creator><description>&lt;p&gt;I tried the detach() method, It updates only the parent object(employee) and child objects(subordinates) are never updated because they are discarded when you call detach method. Any Idea, how to attach a multi tier object where the complete object hierarchy is &amp;nbsp;updated, as is expected ?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1744398" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: How to Attach object to a different data context</title><link>http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx#1724411</link><pubDate>Thu, 17 Sep 2009 14:30:30 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1724411</guid><dc:creator>yossy</dc:creator><description>&lt;p&gt;The problem is that if the main entity include entityset, the data in the entityset will not update un the DB.&lt;/p&gt;
&lt;p&gt;Have you a solution for that.&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1724411" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: How to Attach object to a different data context</title><link>http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx#1691843</link><pubDate>Mon, 04 May 2009 22:01:19 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1691843</guid><dc:creator>Eric J. Smith</dc:creator><description>&lt;p&gt;PLINQO implements Detach on all of the entities by default as well as a TON of other features and enhancements. &amp;nbsp;Check it out at PLINQO.com.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1691843" width="1" height="1"&gt;</description></item><item><title>A fantastic site, and brilliant effort.</title><link>http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx#1654484</link><pubDate>Tue, 18 Nov 2008 21:58:05 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1654484</guid><dc:creator>Vardenafil</dc:creator><description>&lt;p&gt;The site very professional! Keep up the good work! Oh yes, one extra comment - maybe you could add more pictures too! So, good luck to your team!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1654484" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: How to Attach object to a different data context</title><link>http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx#1648193</link><pubDate>Thu, 18 Sep 2008 14:29:51 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1648193</guid><dc:creator>Ferenc Mucsi</dc:creator><description>&lt;p&gt;Hi Omar,&lt;/p&gt;
&lt;p&gt;I have spet a lot of time to find the best solution for this trouble:&lt;/p&gt;
&lt;p&gt;Before u starting update just simple get the detached employee object.&lt;/p&gt;
&lt;p&gt;Employee GetDetachedEmpolee(Employee emp_to_edit)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; dataContext db = new dataContext();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; db.DefferesLoadingEnabled = false; //important!!!&lt;/p&gt;
&lt;p&gt; &amp;nbsp;return db.Employees.Where(e=&amp;gt;e.ID == emp_to_edit.ID).SingleOrDefault();&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1648193" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: How to Attach object to a different data context</title><link>http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx#1647616</link><pubDate>Fri, 12 Sep 2008 16:03:47 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1647616</guid><dc:creator>Thurein</dc:creator><description>&lt;p&gt;I need to clone Linq objects so can I detach and add the existing ones as new entities?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1647616" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: How to Attach object to a different data context</title><link>http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx#1620761</link><pubDate>Mon, 12 May 2008 14:13:14 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1620761</guid><dc:creator>Jason</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;could you post the aspx code?&lt;/p&gt;
&lt;p&gt;I have a problem with a formview, when I try to send the entity object as a objectdatasource updateparameter, it is passed as empty object.&lt;/p&gt;
&lt;p&gt;Thanks in advance,&lt;/p&gt;
&lt;p&gt;Jason&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1620761" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: How to Attach object to a different data context</title><link>http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx#1568270</link><pubDate>Thu, 03 Apr 2008 02:10:23 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1568270</guid><dc:creator>Emmanuel Buah</dc:creator><description>&lt;p&gt; Elad, I dont know what your case is but for most people, its the attach that blows up. So you dont even get to define what your RefreshMode is. RefreshMode is also meant for resolving insert, update conflicts. In any case, Omar, you method works except that you have to &amp;nbsp;detach, attach &amp;quot;before&amp;quot; making the changes that needs to be submitted. Most people were making the changes before detaching and attaching in which case, the context still doesnt know about the changes.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1568270" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: How to Attach object to a different data context</title><link>http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx#1500512</link><pubDate>Thu, 07 Feb 2008 04:37:34 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1500512</guid><dc:creator>Rob</dc:creator><description>&lt;p&gt;Hi Omar,&lt;/p&gt;
&lt;p&gt;Just bought your book and it looks ace thanks.&lt;/p&gt;
&lt;p&gt;I was wondering if you could elaborate on the LINQ multitier solution you came up with in there. &amp;nbsp;You&amp;#39;re using XMLSerialisation to detach the object, then reform it without any attachments (because the contract was disregarded during the serialisation process).&lt;/p&gt;
&lt;p&gt;Is this actually a better way than this post - which I can&amp;#39;t get to work either way! &amp;nbsp;For example, in your book code you&amp;#39;ve passed an object directly to XMLSerialiser which you need to pass the type instead etc.&lt;/p&gt;
&lt;p&gt;Rob&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1500512" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: How to Attach object to a different data context</title><link>http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx#1436459</link><pubDate>Fri, 04 Jan 2008 07:48:44 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1436459</guid><dc:creator>SysfuroSopdof</dc:creator><description>&lt;p&gt;Hello, &lt;/p&gt;
&lt;p&gt;Is everything fine? How are you? &lt;/p&gt;
&lt;p&gt;------------------ &lt;/p&gt;
&lt;p&gt;[url=&lt;a rel="nofollow" target="_new" href="http://planyourshopping.com"&gt;http://planyourshopping.com&lt;/a&gt;]shopping[/url]&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1436459" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: How to Attach object to a different data context</title><link>http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx#1388239</link><pubDate>Sun, 09 Dec 2007 09:58:46 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1388239</guid><dc:creator>omar</dc:creator><description>&lt;p&gt;Andreas: &lt;/p&gt;
&lt;p&gt;Not yet. I am not yet convinced Linq to SQL is good for multitier environment. DataContext holds reference to each and every Linq object it produces in order to track changes via PropertyChanging event. Event handlers are by-directional strong reference. That means both the object and the data context are holding reference to each other all the time. It's scary!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1388239" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: How to Attach object to a different data context</title><link>http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx#1388226</link><pubDate>Sun, 09 Dec 2007 09:53:49 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1388226</guid><dc:creator>omar</dc:creator><description>&lt;p&gt;I corrected the code. There was a mistake on this line:&lt;/p&gt;
&lt;p&gt;this.PropertyChanged = this.PropertyChanging = null;&lt;/p&gt;
&lt;p&gt;Each of these need to be individually set to null.&lt;/p&gt;
&lt;p&gt;this.PropertyChanged = null;&lt;/p&gt;
&lt;p&gt;this.PropertyChanging = null;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1388226" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: How to Attach object to a different data context</title><link>http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx#1388038</link><pubDate>Sun, 09 Dec 2007 07:35:59 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1388038</guid><dc:creator>jalam</dc:creator><description>&lt;p&gt;Omar, it would be better if you could place the souce codes with IDE colors. There are some free tools available on the web. Thanks &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1388038" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: How to Attach object to a different data context</title><link>http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx#1387556</link><pubDate>Sun, 09 Dec 2007 00:34:27 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1387556</guid><dc:creator>jdn</dc:creator><description>&lt;p&gt;Also, this error is throw on build:&lt;/p&gt;
&lt;p&gt;Cannot implicitly convert type 'System.ComponentModel.PropertyChangingEventHandler' to 'System.ComponentModel.PropertyChangedEventHandler'&lt;/p&gt;
&lt;p&gt;on this line:&lt;/p&gt;
&lt;p&gt;this.PropertyChanged = this.PropertyChanging = null;&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1387556" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: How to Attach object to a different data context</title><link>http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx#1387546</link><pubDate>Sun, 09 Dec 2007 00:20:51 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1387546</guid><dc:creator>jdn</dc:creator><description>&lt;p&gt;I have an Address object, which has an AddressType object, a state object, and a country object. &amp;nbsp;These child objects are loaded through Get methods that populate them through other data contexts.&lt;/p&gt;
&lt;p&gt;When I new an Address object, assign it its properties, and try to save it, I get the exception mentioned. &amp;nbsp;Are you saying I have to detach the AddressType, State and Country objects separately? &amp;nbsp;&lt;/p&gt;
&lt;p&gt;1) This is crazy.&lt;/p&gt;
&lt;p&gt;2) I could have multiple key relationships for State (for instance). &amp;nbsp;Are you saying I have to default every single foreign key?&lt;/p&gt;
&lt;p&gt;3) It still doesn't seem to work, I can't save a new address without getting the exception.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1387546" width="1" height="1"&gt;</description></item><item><title>re: Linq to SQL: How to Attach object to a different data context</title><link>http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx#1386890</link><pubDate>Sat, 08 Dec 2007 14:53:29 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1386890</guid><dc:creator>Andreas</dc:creator><description>&lt;p&gt;Hi omar,&lt;/p&gt;
&lt;p&gt;out of curiosity, are you using linq in any big projects so far?&lt;/p&gt;
&lt;p&gt;Best Regards,&lt;/p&gt;
&lt;p&gt;Andreas&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1386890" width="1" height="1"&gt;</description></item></channel></rss>