Читати англійською Редагувати

Поділитися через


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.

C#
public void AttachAll<TSubEntity>(System.Collections.Generic.IEnumerable<TSubEntity> entities) where TSubEntity : TEntity;

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

.NET Framework 4.8.1 та інші версії
Продукт Версії
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

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

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

C#
public void AttachAll<TSubEntity>(System.Collections.Generic.IEnumerable<TSubEntity> entities, bool asModified) where TSubEntity : TEntity;

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

C#
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();
}

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.

C#
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();
}

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.

C#
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();
}

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

.NET Framework 4.8.1 та інші версії
Продукт Версії
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1