LINQ to SQL - Update an object using a different DataContext<p align=left><font face=Arial size=2>Hi Guys, </font></p> <p align=left> </p> <p align=left>Maybe it is just me (I am used to DataSets which hold their own state...) but the whole Attached/Dettached objects in LINQ to SQL is a bit confusing. None of the examples I have read all over the net seemed to help me with this problem:</p> <p align=left> </p> <p align=left>Consider the Products table (Northwind).</p> <p align=left> </p> <p align=left>I have a DAL which is stateless. DataContexts are being recreated over and over again (For each logic opertaion...like I used to do with SQL connections)</p> <p align=left> </p> <p align=left>I would like to simulate the following scenario:</p> <p align=left> </p> <p align=left>1) GetProduct(int id)</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left>Create DataContext</p> <p align=left>Query table</p> <p align=left>Dispose DataContext</p> <p align=left>Return result</p> <p align=left> </p></blockquote> <p dir=ltr align=left>2) Modify Product (lets say that this is done in my client)</p> <p dir=ltr align=left> </p> <p dir=ltr align=left>3) UpdateProduct (Product prod)</p> <blockquote dir=ltr style="margin-right:0px"> <p dir=ltr align=left>Create DataContext</p> <p dir=ltr align=left>Attach the object</p> <p dir=ltr align=left>Submit Changes</p> <p dir=ltr align=left>Dispose DataContext</p> <p dir=ltr align=left> </p></blockquote> <p dir=ltr align=left>This is not working... I tried dettaching the updated object, I even added a timestamp column, but still it doesnt work...</p> <p dir=ltr align=left> </p> <p dir=ltr align=left>How should I do it using LINQ??</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left> </p> <p align=left> </p> <p align=left> </p></blockquote>© 2009 Microsoft Corporation. All rights reserved.Fri, 30 Oct 2009 07:31:17 Z6f42d725-4540-4044-be86-afc7bc2d2b46http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#6f42d725-4540-4044-be86-afc7bc2d2b46http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#6f42d725-4540-4044-be86-afc7bc2d2b46Yoni Mazarhttp://social.msdn.microsoft.com/Profile/en-US/?user=Yoni%20MazarLINQ to SQL - Update an object using a different DataContext<p align=left><font face=Arial size=2>Hi Guys, </font></p> <p align=left> </p> <p align=left>Maybe it is just me (I am used to DataSets which hold their own state...) but the whole Attached/Dettached objects in LINQ to SQL is a bit confusing. None of the examples I have read all over the net seemed to help me with this problem:</p> <p align=left> </p> <p align=left>Consider the Products table (Northwind).</p> <p align=left> </p> <p align=left>I have a DAL which is stateless. DataContexts are being recreated over and over again (For each logic opertaion...like I used to do with SQL connections)</p> <p align=left> </p> <p align=left>I would like to simulate the following scenario:</p> <p align=left> </p> <p align=left>1) GetProduct(int id)</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left>Create DataContext</p> <p align=left>Query table</p> <p align=left>Dispose DataContext</p> <p align=left>Return result</p> <p align=left> </p></blockquote> <p dir=ltr align=left>2) Modify Product (lets say that this is done in my client)</p> <p dir=ltr align=left> </p> <p dir=ltr align=left>3) UpdateProduct (Product prod)</p> <blockquote dir=ltr style="margin-right:0px"> <p dir=ltr align=left>Create DataContext</p> <p dir=ltr align=left>Attach the object</p> <p dir=ltr align=left>Submit Changes</p> <p dir=ltr align=left>Dispose DataContext</p> <p dir=ltr align=left> </p></blockquote> <p dir=ltr align=left>This is not working... I tried dettaching the updated object, I even added a timestamp column, but still it doesnt work...</p> <p dir=ltr align=left> </p> <p dir=ltr align=left>How should I do it using LINQ??</p> <blockquote dir=ltr style="margin-right:0px"> <p align=left> </p> <p align=left> </p> <p align=left> </p></blockquote>Wed, 02 Jan 2008 13:11:00 Z2008-01-21T17:02:26Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#3869a35d-8fc3-482e-b461-672e71a2c3e7http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#3869a35d-8fc3-482e-b461-672e71a2c3e7Mathew Charleshttp://social.msdn.microsoft.com/Profile/en-US/?user=Mathew%20CharlesLINQ to SQL - Update an object using a different DataContextMake sure you're using the Attach(object entity, bool asModified) overload, specifying true.  Otherwise you're attaching the entity as unmodified.  Also, if you're not using a timestamp, you'll have to manage ferrying the original values from client to server yourself, and use the Attach(object entity, object original) overload. <p align=left><font face=Arial size=2></font> </p>Wed, 02 Jan 2008 19:40:07 Z2008-01-02T19:40:07Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#bb23049a-d9df-4b5c-8ce4-f27d4b8d56c8http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#bb23049a-d9df-4b5c-8ce4-f27d4b8d56c8Yoni Mazarhttp://social.msdn.microsoft.com/Profile/en-US/?user=Yoni%20MazarLINQ to SQL - Update an object using a different DataContext<p>No...It doesn't work for me....</p> <p align=left> </p> <p align=left>If I try to attach the object with the asModified overload (after I added a timestamp to the DB and readded the table to the dbml file) I get this error:</p> <p align=left> </p> <p align=left>&quot;An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext.  This is not supported.&quot;</p> <p align=left> </p> <p align=left>I tried all the variations of using Attach (with all overloads) and it still doesnt work...</p> <p align=left> </p> <p align=left>Can someone add a code snippet that works?</p> <p align=left> </p> <p align=left>Thanks</p> <p align=left> </p> <p align=left> </p> <p align=left> </p> <p align=left> </p> <p align=left><font face=Arial size=2></font> </p>Sun, 06 Jan 2008 11:25:56 Z2008-01-06T11:25:56Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#b90823fc-a1c6-47f1-ba6b-b40ababfaf2chttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#b90823fc-a1c6-47f1-ba6b-b40ababfaf2cDaniel Simmons - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Daniel%20Simmons%20-%20MSFTLINQ to SQL - Update an object using a different DataContext<p>I'm not a LINQ to SQL expert (yet), but as I understand it, you can't ever attach an entity to a different context if it has previously been part of a context.  You can make a copy of that entity and attach it to a new context (which is often the case in multi-tier scenarios where you serialize an entity to some other tier and then back and then you take the deserialized thing that came back and attach it to a context--but this is a different instance than what you originally started with).  My impression is that this is a pretty fundamental design decision around the way linq to sql works--how it keeps track of things that need implicitly loaded vs. have already been loaded, etc.</p> <p align=left> </p> <p align=left>- Danny</p> <p align=left><font face=Arial size=2></font> </p>Sun, 06 Jan 2008 20:37:12 Z2008-01-06T20:37:12Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#16bfc7ce-dfbb-4038-a2bf-d47470620e86http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#16bfc7ce-dfbb-4038-a2bf-d47470620e86G-Unithttp://social.msdn.microsoft.com/Profile/en-US/?user=G-UnitLINQ to SQL - Update an object using a different DataContext<p align=left><font face=Arial size=2></font> </p> <p>Check this out</p> <p align=left><a title="http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx" href="http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx">http://msmvps.com/blogs/omar/archive/2007/12/08/linq-to-sql-how-to-attach-object-to-a-different-data-context.aspx</a></p>Sun, 20 Jan 2008 19:03:12 Z2008-01-20T19:03:12Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#a67779a6-6eb1-43e5-8c7e-52a6f7b5cb48http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#a67779a6-6eb1-43e5-8c7e-52a6f7b5cb48Yoni Mazarhttp://social.msdn.microsoft.com/Profile/en-US/?user=Yoni%20MazarLINQ to SQL - Update an object using a different DataContextIt works - meaning it allows you to attach an entity which was queried using another data context to a different data context, BUT I the updates I applied to the object were not monitored (because it is detached) therefore - you have to re-attach the object and only after that to apply the changes - in my opinion this is a big limitation. <p align=left><font face=Arial size=2></font> </p>Mon, 21 Jan 2008 09:38:30 Z2008-01-21T09:38:30Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#0b34399e-a856-4140-a100-60b7a570e538http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#0b34399e-a856-4140-a100-60b7a570e538G-Unithttp://social.msdn.microsoft.com/Profile/en-US/?user=G-UnitLINQ to SQL - Update an object using a different DataContext<p align=left><font face=Arial size=2></font> </p> <p>Yeah i agree with that! </p> <p align=left>I spent at least a day meaning 10 hours. Trying everything i could think of...</p> <p align=left>Updating using a linq model is probably the weekest thing i have seen in the linq to sql model so far. For some reason i really feel like i am missing something...WIll let you know if i find something. Please do the same. </p>Mon, 21 Jan 2008 16:46:38 Z2008-01-21T16:46:38Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#6659589f-4680-4b29-84bb-950c7a433181http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#6659589f-4680-4b29-84bb-950c7a433181Matt Warren - MSFThttp://social.msdn.microsoft.com/Profile/en-US/?user=Matt%20Warren%20-%20MSFTLINQ to SQL - Update an object using a different DataContext<p>The Attach methods are designed to be used strictly for multi-tier round-tripping where the objects you attach back are never the same instances as previously retrieved. This is a deviation from the normal pattern of usage which is to keep the DataContext alive for the entire unit of work that you are performing. </p> <p align=left> </p> <p align=left>Since the objects don't ferry their own changes, you have to invent a transport or API that does this and then supply the change information to the DataContext before you attempt to submit your changes back to the database.  This could be as simple as having a web method that recieves an entity plus a few additional changed values, one that recieves two distinct entities (old and new) or one that recieves only the changed entity yet has a version field that can be used instead of the original values that would otherwise be needed to perform the appropriate optimistic concurrency checks.</p> <p align=left> </p> <p align=left>To use LINQ to SQL correctly, you should maintain a stateful DAL, unless you are actually moving data across physical or process boundary tiers.</p> <p align=left><font face=Arial size=2></font> </p>Mon, 21 Jan 2008 16:51:26 Z2008-01-21T17:02:26Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#34f5631b-271a-4797-8f60-8eda8cfb53e7http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#34f5631b-271a-4797-8f60-8eda8cfb53e7Yoni Mazarhttp://social.msdn.microsoft.com/Profile/en-US/?user=Yoni%20MazarLINQ to SQL - Update an object using a different DataContext<p align=left><font face=Arial size=2>There is one thing that can be done...</font></p> <p align=left> </p> <p align=left>If you query for the object and then, even before you dispose the dataContext you detach it from the data context, you can work with it in disconnected mode. Later on, you will be able to attach the object and apply changes using Attach as modified signature. It will work if you have a timestamp in that table. the problem here - the update query will be bloated (all columns will be set) and by working in disconnected mode - you lose the advantages of LINQ (deferred loading) and stay with an object representation of a table.</p> <p align=left> </p>Mon, 21 Jan 2008 17:01:10 Z2008-01-21T17:01:10Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#aa398ae5-f4ef-47de-9bce-807033797fb1http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#aa398ae5-f4ef-47de-9bce-807033797fb1JRMcFetridgehttp://social.msdn.microsoft.com/Profile/en-US/?user=JRMcFetridgeLINQ to SQL - Update an object using a different DataContext<p align=left><font face=Arial size=2>I think the real issue is that in a web application you have no choice but being disconnected unless someone can show me an alternative. For Linq to SQL to be used in a web application which many of us write CRUD must be simple and efficient (bloated SQL will not do). I have had no problem using it in n-Tier non-web apps but web apps are looking scary. I have experimented as follows using the info in this article <a title="http://msdn2.microsoft.com/en-us/library/bb546187.aspx" href="http://msdn2.microsoft.com/en-us/library/bb546187.aspx">http://msdn2.microsoft.com/en-us/library/bb546187.aspx</a></font><span style="font-size:10pt;font-family:Arial"> and used the Timestamp method</span></p> <p align=left><span style="font-size:10pt;font-family:Arial"></span> </p> <p align=left><span style="font-size:10pt;font-family:Arial">I first created a simple Contacts table:</span></p><span style="font-size:10pt;font-family:Arial"> <p align=left><br>CREATE TABLE [dbo].[Contacts](<br>[ContactID] [int] IDENTITY(1,1) NOT NULL,<br>[Name] [nvarchar](50) NULL,<br>[Version] [timestamp] NOT NULL,<br>CONSTRAINT [PK_Contacts] PRIMARY KEY CLUSTERED <br><br>I use the designer to create my Contacts Entity and then I populate my grid with Contacts and update one of the rows then press the update button that calls my ContactUpdate method in the DAL. It looks like:</span></p><span style="font-size:10pt;font-family:Arial"> <p><br>public void UpdateContact(Contact changedContact)        //<strong><span style="font-family:Arial">contains a changed Name property</span></strong><br>{<br>     NorthwindDataContext db = new NorthwindDataContext();<br>     db.Contacts.Attach(changedContact,true);<br>     db.SubmitChanges();<br><br>}<br><br>on entry I have a new value for Name but I get an exception:<br><strong>System.Data.Linq.ChangeConflictException: Row not found or changed.</strong> </span></p> <p><span style="font-size:10pt;font-family:Arial"></span> </p> <p><span style="font-size:10pt;font-family:Arial">Now I must be missing the obvious here as sure I changed the row or why would I be doing an update! The above</span></p> <p align=left><span style="font-size:10pt;font-family:Arial">article points out other strategys but it is often not feasible to keep around a copy of the old object without of course</span></p> <p align=left><span style="font-size:10pt;font-family:Arial">doing a Query and I hope that is not the answer. This is</span></p> <p><span style="font-size:10pt;font-family:Arial">a key operation if Linq has a hope of being used in a Disconnected mode and it must or else it will be relegated to </span></p> <p><span style="font-size:10pt;font-family:Arial">being used for simple RAD apps. I am surprised that MS does not provide a  good example .</span></p> <p class=MsoNormal style="margin:0in 0in 0pt"><font face="Times New Roman" size=3></font> </p><br>Tue, 22 Jan 2008 15:07:35 Z2008-01-22T15:07:35Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#88331340-7f00-492f-a675-14fef6e55245http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#88331340-7f00-492f-a675-14fef6e55245G-Unithttp://social.msdn.microsoft.com/Profile/en-US/?user=G-UnitLINQ to SQL - Update an object using a different DataContext<p align=left><font face=Arial size=2>Can you point us in the patterns and practice page that shows how to do a proper nteir app using linq ?</font></p> <p align=left>My thought process on this is that you business teirs should be handeling all the information and the presentation teirs should be passing teh objects to the bal and getting the from the bal</p>Tue, 22 Jan 2008 16:42:52 Z2008-01-22T16:42:52Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#b5d73a84-30c2-4cf4-b6b9-aebff5191a57http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#b5d73a84-30c2-4cf4-b6b9-aebff5191a57JRMcFetridgehttp://social.msdn.microsoft.com/Profile/en-US/?user=JRMcFetridgeLINQ to SQL - Update an object using a different DataContext<p align=left><font face=Arial size=2>I cannot point you to the specific patterns and Practices but we have been writing n-Tier apps for a long time and the DAL is the ONLY layer that can do database access. We combine this with a Provider model for the DAL so we can be agile as new database access methods come online such as TableAdapters , N-Hibernate or Linq to SQL. The DAL only returns objects(entitys) to the caller so the DataContext only lives in the DAL and NEVER anywhere else. This also begs the question , is it really proper to return a Linq decorated object as it contains Linq specific attributes. For what it is worth I do not think so so we map to &quot;pure&quot; version of the object. This can be quite painful!</font></p> <p align=left> </p> <p align=left> </p> <p align=left><strong><font size=4>Having said this, the subject is really off topic as I think will really need a good answer on CRUD operations in the</font></strong></p> <p align=left><strong><font size=4>DISCONNECTED world or else LINQ to SQL is useless for web apps</font></strong></p>Tue, 22 Jan 2008 16:51:55 Z2008-01-22T16:51:55Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#bd853eca-005a-4dda-88ae-24869f66269bhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#bd853eca-005a-4dda-88ae-24869f66269bDavid Buchananhttp://social.msdn.microsoft.com/Profile/en-US/?user=David%20BuchananLINQ to SQL - Update an object using a different DataContext<p align=left><font face=Arial size=2><a title="http://msdn2.microsoft.com/en-us/bb546187.aspx" href="http://msdn2.microsoft.com/en-us/bb546187.aspx">http://msdn2.microsoft.com/en-us/bb546187.aspx</a></font></p>Tue, 22 Jan 2008 17:35:40 Z2008-01-22T17:35:40Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#005518f3-46f3-4fe9-aec3-7918052c4596http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#005518f3-46f3-4fe9-aec3-7918052c4596JRMcFetridgehttp://social.msdn.microsoft.com/Profile/en-US/?user=JRMcFetridgeLINQ to SQL - Update an object using a different DataContext<p align=left><font face=Arial size=2>I found the answer to my problem , thanks to Steve Michelotti's excellent post <a title="http://geekswithblogs.net/michelotti/archive/2007/12/30/118076.aspx" href="http://geekswithblogs.net/michelotti/archive/2007/12/30/118076.aspx">http://geekswithblogs.net/michelotti/archive/2007/12/30/118076.aspx</a>. The Timestamp was not being persisted by the Grid and is returned as null. I just did a &quot;dirty&quot; fix to perserve it and the update works</font></p> <p align=left><font size=3>fine . </font></p> <p align=left><font size=3></font> </p> <p align=left><font size=3>Does anyone have any idea how Submitchanges uses the Timestamp</font>.</p>Tue, 22 Jan 2008 18:00:36 Z2008-01-22T18:00:36Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#6b5935b2-fc75-45fe-91bd-0739630d60c9http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#6b5935b2-fc75-45fe-91bd-0739630d60c9G-Unithttp://social.msdn.microsoft.com/Profile/en-US/?user=G-UnitLINQ to SQL - Update an object using a different DataContext<p align=left><font face=Arial size=2></font> </p> <p align=left>Funny i was looking at th post as you posted this</p> <p>Timestamp -&gt; is a var name in this case and a sql type</p> <p align=left><a title="http://geekswithblogs.net/michelotti/archive/2007/12/18/117823.aspx" href="http://geekswithblogs.net/michelotti/archive/2007/12/18/117823.aspx">http://geekswithblogs.net/michelotti/archive/2007/12/18/117823.aspx</a></p> <p align=left>same dude new post.. attach checks to see if someone else updated the same row before if not then updates else through exception</p> <p align=left>Sql Time stamp is auto updated evertime a insert or update is made to tha coresponding row.</p> <p align=left>  </p>Tue, 22 Jan 2008 18:47:54 Z2008-01-22T18:47:54Zhttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#56c3c7c4-e676-4e63-b8d0-90a7603c496chttp://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6f42d725-4540-4044-be86-afc7bc2d2b46#56c3c7c4-e676-4e63-b8d0-90a7603c496cMaxi Nghttp://social.msdn.microsoft.com/Profile/en-US/?user=Maxi%20NgLINQ to SQL - Update an object using a different DataContext<p>Hi Matt,<br/><br/>I am interested in stateful DAL.<br/>How do I maintain a stateful DAL in a web farm distributed cache structure?<br/>If there is, that will same a lot of time working around this problem.<br/><br/>thanks.</p>Fri, 30 Oct 2009 07:31:16 Z2009-10-30T07:31:16Z