Uređivanje

Dijeli putem


Table<TEntity>.AttachAll Method

Definition

Attaches all entities of a collection to the DataContext in either a modified or unmodified state.

Overloads

AttachAll<TSubEntity>(IEnumerable<TSubEntity>)

Attaches all entities of a collection to the DataContext in either a modified or unmodified state.

AttachAll<TSubEntity>(IEnumerable<TSubEntity>, Boolean)

Attaches all entities of a collection to the DataContext in either a modified or unmodified state.

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.

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

AttachAll<TSubEntity>(IEnumerable<TSubEntity>)

Attaches all entities of a collection to the DataContext in either a modified or unmodified state.

public:
generic <typename TSubEntity>
 where TSubEntity : TEntity void AttachAll(System::Collections::Generic::IEnumerable<TSubEntity> ^ entities);
public void AttachAll<TSubEntity> (System.Collections.Generic.IEnumerable<TSubEntity> entities) where TSubEntity : TEntity;
member this.AttachAll : seq<#'Entity> -> unit
Public Sub AttachAll(Of TSubEntity As TEntity) (entities As IEnumerable(Of TSubEntity))

Type Parameters

TSubEntity

The type of entities to attach.

Parameters

entities
IEnumerable<TSubEntity>

The collection of entities.

Remarks

This method attaches all entities of a collection to a new DataContext. 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).

Applies to

AttachAll<TSubEntity>(IEnumerable<TSubEntity>, Boolean)

Attaches all entities of a collection to the DataContext in either a modified or unmodified state.

public:
generic <typename TSubEntity>
 where TSubEntity : TEntity void AttachAll(System::Collections::Generic::IEnumerable<TSubEntity> ^ entities, bool asModified);
public void AttachAll<TSubEntity> (System.Collections.Generic.IEnumerable<TSubEntity> entities, bool asModified) where TSubEntity : TEntity;
member this.AttachAll : seq<#'Entity> * bool -> unit
Public Sub AttachAll(Of TSubEntity As TEntity) (entities As IEnumerable(Of TSubEntity), asModified As Boolean)

Type Parameters

TSubEntity

The type of entities to attach.

Parameters

entities
IEnumerable<TSubEntity>

The collection of entities.

asModified
Boolean

true if the object has a timestamp or RowVersion member; false if original values are being used for the optimistic concurrency check.

Examples

The following example shows how you can update an Order object on a different DataContext instance. The example assumes that you have a connection to a database and have made a LINQ to SQL file for it (in this case, the Northwind sample database).

using (Northwnd db = new Northwnd(@"c:\northwnd.mdf"))
{
    // Get original Customer from deserialization.
    var q1 = db.Orders.First();
    string serializedQ = SerializeHelper.Serialize(q1);
    var q2 = SerializeHelper.Deserialize(serializedQ, q1);

    // Track this object for an update (not insert).
    db.Orders.Attach(q2, false);

    // Replay the changes.
    q2.ShipRegion = "King";
    q2.ShipAddress = "1 Microsoft Way";

    // DataContext knows how to update the order.
    db.SubmitChanges();
}
Using db As New Northwnd("")
    ' Get original Customer from deserialization.
    Dim q1 = db.Orders.First()
    Dim serializedQ As String = SerializeHelper.Serialize(q1)
    Dim q2 = SerializeHelper.Deserialize(serializedQ, q1)

    ' Track this object for an update (not insert).
    db.Orders.Attach(q2, False)

    ' Replay the changes.
    q2.ShipRegion = "King"
    q2.ShipAddress = "1 Microsoft Way"

    ' DataContext knows how to update the order.
    db.SubmitChanges()
End Using

In the following example, an entity object to be attached has a foreign key relation with another object and is stored in the cache but not attached. When you call SubmitChanges, the ChangeProcessor adds an Insert operation for all the foreign key objects. This is a side-effect when an entity instance is re-used in a different DataContext instance. For this reason, LINQ to SQL does not support re-use of objects.

Customer c = null;
using (Northwnd db = new Northwnd(""))
{
    /* Get both the customer c and the customer's order
    into the cache. */
    c = db.Customers.First();
    string sc = c.Orders.First().ShipCity;
}

using (Northwnd nw2 = new Northwnd(""))
{
    // Attach customers and update the address.
    nw2.Customers.Attach(c, false);
    c.Address = "new";
    nw2.Log = Console.Out;

    /* At SubmitChanges, you will see INSERT requests for all
    Customer c’s orders. */
    nw2.SubmitChanges();
}
Sub method7()
    Dim c As Customer = Nothing
    Using db = New Northwnd("...")
        ' Get both the customer c and the customer's order
        ' into the cache.
        c = db.Customers.First()
        Dim sc = c.Orders.First().ShipCity
    End Using

    Using nw2 = New Northwnd("...")
        ' Attach customers and update the address.
        nw2.Customers.Attach(c, False)
        c.Address = "new"
        nw2.Log = Console.Out

        ' At SubmitChanges, you will see INSERT requests for all
        ' c's orders.
        nw2.SubmitChanges()
    End Using

The following example shows a scenario in which Customer A has canceled all orders and Customer B has taken ownership of them. You can attach all orders of Customer A at the same time.

Customer CustA_File = new Customer();
Customer CustB_File = new Customer();
string xmlFileA = "";
string xmlFileB = "";

// Get the serialized objects.
Customer A = SerializeHelper.Deserialize<Customer>(xmlFileA, CustA_File);
Customer B = SerializeHelper.Deserialize<Customer>(xmlFileB, CustB_File);
List<Order> AOrders = A.Orders.ToList();

using (Northwnd db = new Northwnd(@"c:\northwnd.mdf"))

{
    //Attach all the orders belonging to Customer A all at once.
    db.Orders.AttachAll(AOrders, false);

    // Update the orders belonging to Customer A to show them
    // as owned by Customer B.
    foreach (Order o in AOrders)
    {
        o.CustomerID = B.CustomerID;
    }

    // DataContext can now apply the change of ownership to
    // the database.
    db.SubmitChanges();
}
Dim custA_File = New Customer()
Dim custB_File = New Customer()
Dim xmlFileA As String = ""
Dim xmlFileB As String = ""

' Get the serialized objects.
Dim A As Customer = SerializeHelper.Deserialize(Of Customer)(xmlFileA, custA_File)
Dim B As Customer = SerializeHelper.Deserialize(Of Customer)(xmlFileB, custB_File)

Dim AOrders As List(Of Order) = A.Orders.ToList()

Using db As New Northwnd("...")
    'Attach all the orders belonging to Customer A all at once.
    db.Orders.AttachAll(AOrders, False)

    ' Update the orders belonging to Customer A to show them
    ' as owned by Customer B
    For Each o In AOrders
        o.CustomerID = B.CustomerID
    Next

    ' DataContext can now apply the change of ownership to
    'the database
    db.SubmitChanges()
End Using

Remarks

This method attaches all entities of a collection to the DataContext in either a modified or unmodified state. If attaching as modified, the entity must either declare a version member or must not participate in update conflict checking. If attaching as unmodified, the entity is assumed to represent the original value. After calling this method, the entity's fields can be modified with other information from the client before SubmitChanges is called. For more information, see Data Retrieval and CUD Operations in N-Tier Applications (LINQ to SQL).

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