Table<TEntity>.AttachAll 메서드

정의

컬렉션의 모든 항목을 수정되었거나 수정되지 않은 상태의 DataContext에 연결합니다.

오버로드

AttachAll<TSubEntity>(IEnumerable<TSubEntity>)

컬렉션의 모든 항목을 수정되었거나 수정되지 않은 상태의 DataContext에 연결합니다.

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

컬렉션의 모든 항목을 수정되었거나 수정되지 않은 상태의 DataContext에 연결합니다.

설명

수정된 대로 연결하는 경우 엔터티는 버전 멤버를 선언하거나 업데이트 충돌 검사에 참여해서는 안 됩니다.

새 엔터티가 연결되면 모든 자식 컬렉션(예 EntitySet : 연결된 테이블의 엔터티 컬렉션)에 대한 지연 로더가 초기화됩니다. SubmitChanges 가 호출되면 자식 컬렉션의 멤버가 상태에 놓입니다Unmodified. 자식 컬렉션의 멤버를 업데이트하려면 명시적으로 를 호출 Attach 하고 해당 엔터티를 지정해야 합니다.

자세한 내용은 데이터 검색 및 CUD 작업에서 N 계층 애플리케이션 (LINQ to SQL)합니다.

AttachAll<TSubEntity>(IEnumerable<TSubEntity>)

컬렉션의 모든 항목을 수정되었거나 수정되지 않은 상태의 DataContext에 연결합니다.

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

형식 매개 변수

TSubEntity

연결할 엔터티의 형식입니다.

매개 변수

entities
IEnumerable<TSubEntity>

엔터티의 컬렉션입니다.

설명

이 메서드는 컬렉션의 모든 엔터티를 새 DataContext에 연결합니다. 새 엔터티가 연결되면 모든 자식 컬렉션(예 EntitySet : 연결된 테이블의 엔터티 컬렉션)에 대한 지연 로더가 초기화됩니다. SubmitChanges 가 호출되면 자식 컬렉션의 멤버가 상태에 놓입니다Unmodified. 자식 컬렉션의 멤버를 업데이트하려면 명시적으로 를 호출 Attach 하고 해당 엔터티를 지정해야 합니다.

자세한 내용은 데이터 검색 및 CUD 작업에서 N 계층 애플리케이션 (LINQ to SQL)합니다.

적용 대상

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

컬렉션의 모든 항목을 수정되었거나 수정되지 않은 상태의 DataContext에 연결합니다.

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

형식 매개 변수

TSubEntity

연결할 엔터티의 형식입니다.

매개 변수

entities
IEnumerable<TSubEntity>

엔터티의 컬렉션입니다.

asModified
Boolean

개체에 타임스탬프 또는 RowVersion 멤버가 있으면 true이고, 낙관적 동시성 검사를 위해 원래 값이 사용되고 있으면 false입니다.

예제

다음 예제에서는 다른 DataContext instance 개체를 업데이트하는 Order 방법을 보여줍니다. 이 예제에서는 데이터베이스에 대한 연결이 있고 데이터베이스에 대한 LINQ to SQL 파일을 만들었다고 가정합니다(이 경우 Northwind 샘플 데이터베이스).

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

다음 예제에서 연결할 엔터티 개체는 다른 개체와 외래 키 관계를 하며 캐시에 저장되지만 연결되지는 않습니다. 를 호출 SubmitChanges할 때 는 ChangeProcessor 모든 외래 키 개체에 대한 작업을 추가 Insert 합니다. 이는 instance 엔터티가 다른 DataContext instance 다시 사용되는 경우의 부작용입니다. 이러한 이유로 LINQ to SQL 개체의 재사용을 지원하지 않습니다.

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

다음 예제에서는 고객 A가 모든 주문을 취소하고 고객 B가 소유권을 가져온 시나리오를 보여 줍니다. 고객 A의 모든 주문을 동시에 연결할 수 있습니다.

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

설명

이 메서드는 수정되거나수정되지 않은 상태에서 컬렉션 DataContext 의 모든 엔터티를 에 연결합니다. 수정된 대로 연결하는 경우 엔터티는 버전 멤버를 선언하거나 업데이트 충돌 검사에 참여해서는 안 됩니다. 수정되지 않은 것으로 연결하는 경우 엔터티는 원래 값을 나타내는 것으로 간주됩니다. 이 메서드를 호출한 후에는 가 호출되기 전에 SubmitChanges 클라이언트의 다른 정보로 엔터티의 필드를 수정할 수 있습니다. 자세한 내용은 데이터 검색 및 CUD 작업에서 N 계층 애플리케이션 (LINQ to SQL)합니다.

새 엔터티가 연결되면 모든 자식 컬렉션(예 EntitySet : 연결된 테이블의 엔터티 컬렉션)에 대한 지연 로더가 초기화됩니다. SubmitChanges 가 호출되면 자식 컬렉션의 멤버가 상태에 놓입니다Unmodified. 자식 컬렉션의 멤버를 업데이트하려면 명시적으로 를 호출 Attach 하고 해당 엔터티를 지정해야 합니다.

적용 대상

.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