Table<TEntity>.Attach Table<TEntity>.Attach Table<TEntity>.Attach Table<TEntity>.Attach Method

Definition

Attaches an entity to the DataContext.

Overloads

Attach(TEntity) Attach(TEntity) Attach(TEntity) Attach(TEntity)

Attaches a disconnected or "detached" entity to a new DataContext when original values are required for optimistic concurrency checks.

Attach(TEntity, Boolean) Attach(TEntity, Boolean) Attach(TEntity, Boolean) Attach(TEntity, Boolean)

Attaches an entity to the DataContext in either a modified or unmodified state.

Attach(TEntity, TEntity) Attach(TEntity, TEntity) Attach(TEntity, TEntity) Attach(TEntity, TEntity)

Attaches an entity to the DataContext in either a modified or unmodified state by specifying both the entity and its original state.

Remarks

Use the Attach methods with entities that have been created in one DataContext, serialized to a client, and then deserialized back (with the intention to perform an update or delete operation). For more information, see Data Retrieval and CUD Operations in N-Tier Applications (LINQ to SQL).

Do not try to Attach an entity that has not been detached through serialization. Entities that have not been serialized still maintain associations with deferred loaders that can cause unexpected results if the entity becomes tracked by a second data context.

When a new entity is attached, deferred loaders for any child collections (for example, EntitySet collections of entities from associated tables) are initialized. When SubmitChanges is called, members of the child collections are put into an Unmodified state. To update members of a child collection, you must explicitly call Attach and specify that entity.

Attach attaches all entities in the object graph of the provided object. For example, the following C# code:

using (SampleDataContext db = new SampleDataContext()) {  
    Employee employee = new Employee { employeeId = 1 };  

    Master master = new Master();  
    master.Employee = employee;  

    Child child = new Child();  
    child.Employee = employee;  

    db.Employees.Attach(employee);  

    master.Child = child;  

    db.Masters.InsertOnSubmit(master);  

    db.SubmitChanges();  
}  

The equivalent Visual Basic code is:

Using db As New SampleDataContext()    Dim employee As New Employee With { .employeeId = 1 }  

    Dim master As New Master()  
    master.Employee = employee  

    Dim child As New Child()  
    child.Employee = employee  

    db.Employees.Attach(employee)  

    master.Child = child  

    db.Masters.InsertOnSubmit(master)  

    db.SubmitChanges()  

End Using  

Calling Attach on Employee attaches employee, master, and child, because the Employee has relationships to both master and child. You must explicitly call InsertOnSubmit to change the state from attached to inserted.

Attach(TEntity) Attach(TEntity) Attach(TEntity) Attach(TEntity)

Attaches a disconnected or "detached" entity to a new DataContext when original values are required for optimistic concurrency checks.

public:
 virtual void Attach(TEntity entity);
public void Attach (TEntity entity);
abstract member Attach : 'Entity -> unit
override this.Attach : 'Entity -> unit
Public Sub Attach (entity As TEntity)

Parameters

entity
TEntity TEntity TEntity TEntity

The original values of the entity to be attached.

Remarks

Use the Attach methods with entities that have been created in one DataContext, serialized to a client, and then deserialized back to perform an update or delete operation. Because the new DataContext has no way of tracking what the original values were for a disconnected entity, the client is responsible for supplying those values. In this version of Attach, the entity is assumed to be in its original value state. After calling this method, you can then update its fields, for example with additional data sent from the client.

When a new entity is attached, deferred loaders for any child collections (for example, EntitySet collections of entities from associated tables) are initialized. When SubmitChanges is called, members of the child collections are put into an Unmodified state. To update members of a child collection, you must explicitly call Attach and specify that entity.

For more information, see Data Retrieval and CUD Operations in N-Tier Applications (LINQ to SQL).

Do not try to Attach an entity that has not been detached through serialization. Entities that have not been serialized still maintain associations with deferred loaders that can cause unexpected results if the entity becomes tracked by a second data context.

Attach(TEntity, Boolean) Attach(TEntity, Boolean) Attach(TEntity, Boolean) Attach(TEntity, Boolean)

Attaches an entity to the DataContext in either a modified or unmodified state.

public:
 void Attach(TEntity entity, bool asModified);
public void Attach (TEntity entity, bool asModified);
member this.Attach : 'Entity * bool -> unit
Public Sub Attach (entity As TEntity, asModified As Boolean)

Parameters

entity
TEntity TEntity TEntity TEntity

The entity to be attached.

asModified
Boolean Boolean Boolean Boolean

true to attach the entity as modified; false to attach the entity as unmodified.

Remarks

If attaching as modified, the entity must either declare a version member or must not participate in update conflict checking. When a new entity is attached, deferred loaders for any child collections (for example, EntitySet collections of entities from associated tables) are initialized. When SubmitChanges is called, members of the child collections are put into an Unmodified state. To update members of a child collection, you must explicitly call Attach and specify that entity.

Attach(TEntity, TEntity) Attach(TEntity, TEntity) Attach(TEntity, TEntity) Attach(TEntity, TEntity)

Attaches an entity to the DataContext in either a modified or unmodified state by specifying both the entity and its original state.

public:
 void Attach(TEntity entity, TEntity original);
public void Attach (TEntity entity, TEntity original);
member this.Attach : 'Entity * 'Entity -> unit
Public Sub Attach (entity As TEntity, original As TEntity)

Parameters

entity
TEntity TEntity TEntity TEntity

The entity to be attached.

original
TEntity TEntity TEntity TEntity

An instance of the same entity type with data members that contain the original values.

Examples

using (Northwnd db2 = new Northwnd(@"c:\northwnd.mdf"))
{
    Customer Cust_File = new Customer();
    string xmlFile = "";
    
    // Get the original object from the deserializer.
    Customer c = SerializeHelper.Deserialize<Customer>
        (xmlFile, Cust_File);

    // Set all the desired properties to the entity to be attached.
    Customer c_updated = new Customer() { CustomerID = c.CustomerID,
        Phone = "425-123-4567", CompanyName = "Microsoft" };
    db2.Customers.Attach(c_updated, c);

    // Perform last minute updates, which will still take effect. 
    c_updated.Phone = "425-765-4321";

    // SubmitChanges()sets the phoneNumber and CompanyName of
    // customer with customerID=Cust. to "425-765-4321" and
    // "Microsoft" respectively.
    db2.SubmitChanges();  
}
Using db = New Northwnd("...")
    Dim Cust_File As New Customer()
    Dim xmlFile As String = ""

    'Get the original object from the deserializer.
    Dim c As Customer = SerializeHelper.Deserialize(Of Customer)(xmlFile, Cust_File)

    ' Set all the desired properties to the entity to be attached.
    Dim c_updated As New Customer With {.CustomerID = c.CustomerID, _
    .Phone = "425-123-4567", .CompanyName = "Microsoft"}
    db.Customers.Attach(c_updated, c)

    ' Perform last minute updates, which will still take effect. 
    c_updated.Phone = "425-765-4321"

    ' SubmitChanges()sets the phoneNumber and CompanyName of
    ' customer with customerID=Cust. to "425-765-4321" and
    ' "Microsoft" respectively.
    db.SubmitChanges()
End Using

Remarks

In the following example, the Customer object is already correctly configured. You can call Attach without having to replay the updates.

When a new entity is attached, deferred loaders for any child collections (for example, EntitySet collections of entities from associated tables) are initialized. When SubmitChanges is called, members of the child collections are put into an Unmodified state. To update members of a child collection, you must explicitly call Attach and specify that entity.

Applies to