Table<TEntity>.AttachAll 方法

定義

以已修改或未修改的狀態,將集合的所有實體附加至 DataContext

多載

AttachAll<TSubEntity>(IEnumerable<TSubEntity>)

以已修改或未修改的狀態,將集合的所有實體附加至 DataContext

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

以已修改或未修改的狀態,將集合的所有實體附加至 DataContext

備註

如果附加為已修改,實體必須宣告版本成員,或不得參與更新衝突檢查。

附加新實體時,任何子集合的延後載入器 (例如, EntitySet 會初始化相關聯數據表中的實體集合) 。 呼叫 時 SubmitChanges ,子集合的成員會進入 Unmodified 狀態。 若要更新子集合的成員,您必須明確呼叫 Attach 並指定該實體。

如需詳細資訊,請參閱多層式架構應用程式中的資料擷取和 CUD 作業 (LINQ to SQL)

AttachAll<TSubEntity>(IEnumerable<TSubEntity>)

以已修改或未修改的狀態,將集合的所有實體附加至 DataContext

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

類型參數

TSubEntity

要附加的實體型別。

參數

entities
IEnumerable<TSubEntity>

實體的集合。

備註

這個方法會將集合的所有實體附加至新的 DataContext。 附加新實體時,任何子集合的延後載入器 (例如, EntitySet 會初始化相關聯數據表中的實體集合) 。 呼叫 時 SubmitChanges ,子集合的成員會進入 Unmodified 狀態。 若要更新子集合的成員,您必須明確呼叫 Attach 並指定該實體。

如需詳細資訊,請參閱多層式架構應用程式中的資料擷取和 CUD 作業 (LINQ to SQL)

適用於

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

以已修改或未修改的狀態,將集合的所有實體附加至 DataContext

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)

類型參數

TSubEntity

要附加的實體型別。

參數

entities
IEnumerable<TSubEntity>

實體的集合。

asModified
Boolean

如果物件有時間戳記或 RowVersion 成員,則為 true。如果開放式並行存取檢查是使用原始值,則為 false

範例

下列範例示範如何更新 Order 不同 DataContext 實例上的物件。 此範例假設您有資料庫的連線,而且在此案例中 (建立 LINQ to SQL 檔案,即 Northwind 範例資料庫) 。

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

在下列範例中,要附加的實體物件與另一個物件具有外鍵關聯性,而且會儲存在快取中,但不會附加。 當您呼叫 SubmitChanges時,會 ChangeProcessor 新增 Insert 所有外鍵對象的作業。 當實體實例重新用於不同的 DataContext 實例時,這是副作用。 因此,LINQ to SQL 不支援重複使用 物件。

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

下列範例顯示客戶 A 已取消所有訂單,而客戶 B 已取得其擁有權的案例。 您可以同時附加客戶 A 的所有訂單。

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

備註

這個方法會將集合的所有實體附加至 DataContext 處於 已修改未修改狀態的 。 如果附加為已修改,實體必須宣告版本成員,或不得參與更新衝突檢查。 如果附加為未修改,則會假設實體代表原始值。 呼叫這個方法之後,可以先使用用戶端的其他資訊修改實體的字段,然後再 SubmitChanges 呼叫。 如需詳細資訊,請參閱多層式架構應用程式中的資料擷取和 CUD 作業 (LINQ to SQL)

附加新實體時,任何子集合的延後載入器 (例如, EntitySet 會初始化相關聯數據表中的實體集合) 。 呼叫 時 SubmitChanges ,子集合的成員會進入 Unmodified 狀態。 若要更新子集合的成員,您必須明確呼叫 Attach 並指定該實體。

適用於