Quantcast
Channel: dotnetpete - LINQ
Viewing all articles
Browse latest Browse all 4

LINQ to SQL Attach Demons

$
0
0

I have been investigating LINQ to SQL supposed ability to handle disconnected entities. For example, grab a single entity with a data context, dispose of the data context and viola you now have a disconnected entity. By default, you can access properties of that entity that are not other entities themselves. If you try you will get a data context disposed exception. 

LINQ to SQL has a handy method called Attach which is supposed to re-attach entities so you can send changes to the database.  When I called the Attach method I would get the exception:

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.

This seemed to be an issue with entities that had relations to other entities.

Inside the data context that was used to select data, if I set DeferredLoadingEnabled = false I was able to attach OK (specifying true for the asModified parameter). This meant that all related entites were also retrieved but it made the update code a lot simplier.


Viewing all articles
Browse latest Browse all 4

Trending Articles