Table<TEntity>.AttachAll 메서드

정의

컬렉션 DataContext 의 모든 엔터티를 수정되거나 수정되지 않은 상태로 연결합니다.

오버로드

Name Description
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

true 개체에 타임스탬프 또는 RowVersion 멤버가 있는 경우 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

다음 예제에서 연결할 엔터티 개체는 다른 개체와 외래 키 관계를 가지고 있으며 캐시에 저장되지만 연결되지는 않습니다. 호출 SubmitChangesChangeProcessor 할 때 모든 외래 키 개체에 대한 연산을 추가 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 하고 지정해야 합니다.

적용 대상