Editare

Partajați prin


Table<TEntity>.Attach Method

Definition

Attaches an entity to the DataContext.

Overloads

Attach(TEntity)

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

Attach(TEntity, Boolean)

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

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:

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

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

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

    db.Employees.Attach(employee);  

    master.Child = child;  

    db.Masters.InsertOnSubmit(master);  

    db.SubmitChanges();  
} 
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)

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

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

Parameters

entity
TEntity

The original values of the entity to be attached.

Implements

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.

Applies to

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

The entity to be attached.

asModified
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.

Applies to

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

The entity to be attached.

original
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