17

What exactly does the LINQ-to-SQL method Table<T>.Attach() and Table<T>.AttachAll() and what is an example/situation for their proper usage?

Also, please check out this related question: How to detach a LINQ-to-SQL data object from the DataContext's tracking mechanism?

Community
  • 1
  • 1
smartcaveman
  • 41,281
  • 29
  • 127
  • 212

2 Answers2

18

It is really useful in multi-tier applications that serialize/deserialize data to other layers.

Short version:

Attach() tells DataContext the entity is not new (for insert) but an updated entity that is meant to be updated in the DB.

Long version:

You have a DataContext where your entities exist. New entities get inserted, existing ones get updated. Now you need to send some entity to another tier, DataContext then detaches said entity and sends it away.
On the other tier the entity gets modified and sent back to your data layer. Now the former DataContext that had your entity may not exist anymore (eg. if it is stateless) or does not know your deserialized entity so what do you do? You create a new DataContext or use the existing one and use the Attach() method - this way the DataContext knows the entity is meant to be updated and should not be inserted into the database.

The same goes for AttachAll() but for multiple entities.

Jaroslav Jandek
  • 9,463
  • 1
  • 28
  • 30
  • So, it does something like a PK search of the table for a match and updates the record from the detached data object? – smartcaveman Mar 09 '11 at 10:46
  • 1
    @smartcaveman: Yes. DataContext starts tracking the attached entity. Bear in mind that you should not do this for an entity attached to another `DataContext` - use it ONLY for detached entities. Without Attach, your entity would be flagged as new an inserted. Of course the update itself will be performed when you ask for it - not when you call `Attach`. – Jaroslav Jandek Mar 09 '11 at 10:59
  • @smartcaveman: this may explain it for you: it will **flag** the entity for updating, so it will "perform a PK search" instead of a direct "INSERT" when saving occurs. – Jaroslav Jandek Mar 09 '11 at 11:02
  • 1
    Use `Attach(object,True)` (Which requires some version number or disabled tracking) or `Attach(objectModified, objectOriginal)` which works out what has changes, and just does the updates. Just attaching doesn't seem to do anything. – Piotr Kula Apr 04 '14 at 10:12
4

LINQ to SQL maintains the state of entities in a DataContext object. Entities that are loaded from the database are associated with a DataContext that is responsible for tracking any changes to the entity so when you save it the appropriate changes are made to the database.

Entities can become detached from the DataContext when they are serialized (for passing to a client for example in an n-tier application). When the client returns the entity back to your DA layer you will need to reattach it to a DataContext before it can be updated or deleted in the database. The Attach method performs this operation.

Simon P Stevens
  • 27,303
  • 5
  • 81
  • 107