Table<TEntity>.AttachAll メソッド

定義

変更された状態または変更されていない状態の DataContext にコレクションのすべてのエンティティをアタッチします。

オーバーロード

AttachAll<TSubEntity>(IEnumerable<TSubEntity>)

変更された状態または変更されていない状態の DataContext にコレクションのすべてのエンティティをアタッチします。

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

変更された状態または変更されていない状態の DataContext にコレクションのすべてのエンティティをアタッチします。

注釈

変更済みとしてアタッチする場合、エンティティはバージョン メンバーを宣言するか、更新の競合チェックに参加しないようにする必要があります。

新しいエンティティがアタッチされると、子コレクション (たとえば、 EntitySet 関連付けられているテーブルのエンティティのコレクション) の遅延ローダーが初期化されます。 が呼び出されると SubmitChanges 、子コレクションのメンバーは状態になります Unmodified 。 子コレクションのメンバーを更新するには、そのエンティティを明示的に呼び出 Attach して指定する必要があります。

詳細については、「N 層アプリケーションでのデータ取得および 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 して指定する必要があります。

詳細については、「N 層アプリケーションでのデータ取得および 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

次の例は、別DataContextのインスタンスのオブジェクトを更新するOrder方法を示しています。 この例では、データベースへの接続があり、そのデータベースに対して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 に、エンティティのフィールドをクライアントからの他の情報で変更できます。 詳細については、「N 層アプリケーションでのデータ取得および CUD 操作 (LINQ to SQL)」を参照してください。

新しいエンティティがアタッチされると、子コレクション (たとえば、 EntitySet 関連付けられているテーブルのエンティティのコレクション) の遅延ローダーが初期化されます。 が呼び出されると SubmitChanges 、子コレクションのメンバーは状態になります Unmodified 。 子コレクションのメンバーを更新するには、そのエンティティを明示的に呼び出 Attach して指定する必要があります。

適用対象