What does LINQ-to-SQL Table.Attach do?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

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.

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.

– smartcaveman Mar 9 at 10:46 @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 9 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 9 at 11:02.

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.

What does LINQ-to-SQL Table. Why should I use GetOriginalEntityState() in my LINQ To SQL repository save method? How can I reject all changes in a Linq to SQL's DataContext?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions