I was stunned that Microsoft has made disconnected updates with Linq so difficult. Didn’t they realize that a large chunk of their developers would be using ASP.NET or WCF?
Some of the more cryptic errors are:
“An entity can only be attached as modified without original state if it declares a version member or does not have an update check policy.”
“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.”
Changing update policies didn’t work. Adding timestamp columns didn’t work. Crying, while it felt good, didn’t work. What worked? A couple hours of forum crawling just to find one good post, here. Sidar Ok, you are my hero of the day. If I was the ruler of a small country, August 18th would be Sidar Ok Day.
Happiness Returns
This gist of it is to turn off object tracking for the retrieval data context. You would think that there would be no tracking involved after that data context is disposed of, but c’est la vie. Could be that some garbage disposal needed to happen or Linq’s Table attribute is adding some wierd exentsibility. I dunno.
In this sample, I’m using a simple AccountType table. I am retrieving, disconnecting, modifing, and saving.
Sample Code
Sub DisconnectedAccountTypeUpdate()
Dim acctType As Linq2SqlTestApp.DAL.AccountType
Dim originalAcctType As Linq2SqlTestApp.DAL.AccountType‘Retrieve and Disconnect
Using dc As New Linq2SqlTestApp.DAL.MyDatabase(My.Settings.MyDatabaseConnectionString)
dc.Log = Console.Out
dc.ObjectTrackingEnabled = False
acctType = dc.GetTable(Of AccountType).Single(Function(g) g.AccountType1 = “RESOLD”)
originalAcctType = dc.GetTable(Of AccountType).Single(Function(g) g.AccountType1 = “RESOLD”)
End Using‘Modify disconnected object.
acctType.OrderBy += 1‘Update
Using dc As New Linq2SqlTestApp.DAL.MyDatabase(My.Settings.MyDatabaseConnectionString)
dc.Log = Console.Out
dc.AccountType.Attach(acctType, originalAcctType)
dc.SubmitChanges()
End UsingEnd Sub
Posted by Andrew Carver on August 20, 2008 at 2:10 pm
Fascinating. I did not know that…
Posted by Sidar Ok on August 22, 2008 at 7:25 pm
I just discovered this post and felt content
Let’s go set up a country now
I am really happy to have helped.
I also have implemented a disconnected, multitiered solution with WCF and linq to SQL and prettty much covered most of the cases that one would encounter during this in 2 blog posts:
http://www.sidarok.com/web/blog/content/2008/05/26/linq-to-sql-with-wcf-in-a-multi-tiered-action-part-1.html
http://www.sidarok.com/web/blog/content/2008/06/02/linq-to-sql-with-wcf-in-a-multi-tiered-action-part-2.html
It also includes the source codes. Hope you’ll enjoy!
Posted by Sidar Ok on August 22, 2008 at 7:37 pm
Also, on datacontext disposal thing – in this particular example there happens to be one time the two datacontexts are alive, because a disposable object is disposed at the end of a using scope. So at the inner method who is responsible for deletion the mother data context is still alive, and tracking the object to be deleted.
Hence, db context doesnt want to track an object which is already dirty, and already another context is responsible of it. So disabling the object tracking in the readonly context solves the problem.
In some cases the solution might not be that easy, then you may want to put them in seperate data contexts.
Posted by vishal kadiwala on October 18, 2010 at 12:15 pm
hi,
very much thanks for this solution.
It help’s a lot.
Thanks,
Posted by aarkay on February 6, 2012 at 1:19 am
Dude, you are a life saver….after wasting 8-10 hrs on my flukes and…google…I stumbled upon your blog….
thanks
Posted by MadHatter on June 16, 2012 at 9:42 am
To Jamison White and Sidar Ok a HUGE THANK YOU!!! You two are truly life savers…you’re solution helped me to perform CRUD operations on a very very unique table with a very unique PK key which consists out of 5 field composite key without any hassles…
Again, thank you and really appreciated!!