방법: EntityReference를 사용하여 개체 간 관계 변경(Entity Framework)

이 항목에서는 EntityReference 개체를 사용하여 개체 컨텍스트에서 두 개체 간의 관계를 변경하는 방법을 보여 줍니다. SaveChanges 메서드를 호출하면 관계 변경 사항이 데이터베이스에서 관련된 테이블의 외래 키에 대한 변경 사항으로 보관됩니다. 자세한 내용은 방법: 연결 변경 중 비즈니스 논리 실행을 참조하십시오.

이 항목의 예제는 Adventure Works Sales 모델을 기반으로 합니다. 이 항목의 코드를 실행하려면 프로젝트에 Adventure Works Sales 모델을 추가하고 프로젝트에서 Entity Framework를 사용하도록 구성해야 합니다. 자세한 내용은 방법: 엔터티 데이터 모델 마법사 사용(Entity Framework) 또는 방법: Entity Framework 프로젝트 수동 구성방법: 엔터티 데이터 모델 수동 정의(Entity Framework)를 참조하십시오.

예제

이 예제에서는 EntityReference 개체를 사용하여 SalesOrderHeader 개체와 주문의 배송 주소를 나타내는 관련 Address 개체 간의 관계를 변경하는 방법을 보여 줍니다.

' Define the order and new address IDs. 
Dim orderId As Integer = 43669
Dim addressId As Integer = 26

Using context As New AdventureWorksEntities()
    ' Get the billing address to change to. 
    Dim address As Address = context.Addresses.Single(Function(c) c.AddressID = addressId)

    ' Get the order being changed. 
    Dim order As SalesOrderHeader = context.SalesOrderHeaders.Single(Function(o) o.SalesOrderID = orderId)

    ' You do not have to call the Load method to load the addresses for the order, 
    ' because lazy loading is set to true 
    ' by the constructor of the AdventureWorksEntities object. 
    ' With lazy loading set to true the related objects are loaded when 
    ' you access the navigation property. In this case Address. 

    ' Write the current billing street address. 
    Console.WriteLine("Current street: " & order.Address.AddressLine1)

    ' Change the billing address. 
    If Not order.Address.Equals(address) Then
        ' Use Address navigation property to change the association. 
        order.Address = address

        ' Write the changed billing street address. 
        Console.WriteLine("Changed street: " & order.Address.AddressLine1)
    End If

    ' If the address change succeeds, save the changes. 
    context.SaveChanges()

    ' Write the current billing street address. 
    Console.WriteLine("Current street: " & order.Address.AddressLine1)
End Using
// Define the order and new address IDs.
int orderId = 43669;
int addressId = 26;

using (AdventureWorksEntities context
    = new AdventureWorksEntities())
{
    // Get the billing address to change to.
    Address address =
        context.Addresses.Single(c => c.AddressID == addressId);

    // Get the order being changed.
    SalesOrderHeader order =
        context.SalesOrderHeaders.Single(o => o.SalesOrderID == orderId);

    // You do not have to call the Load method to load the addresses for the order,
    // because  lazy loading is set to true 
    // by the constructor of the AdventureWorksEntities object. 
    // With  lazy loading set to true the related objects are loaded when
    // you access the navigation property. In this case Address.

    // Write the current billing street address.
    Console.WriteLine("Current street: "
        + order.Address.AddressLine1);

    // Change the billing address.
    if (!order.Address.Equals(address))
    {
        // Use Address navigation property to change the association.
        order.Address = address;

        // Write the changed billing street address.
        Console.WriteLine("Changed street: "
            + order.Address.AddressLine1);
    }

    // If the address change succeeds, save the changes.
    context.SaveChanges();

    // Write the current billing street address.
    Console.WriteLine("Current street: "
        + order.Address.AddressLine1);
}

참고 항목

작업

방법: 외래 키 속성을 사용하여 개체 간 관계 변경
방법: 연결 변경 중 비즈니스 논리 실행

개념

개체 사용(Entity Framework)
관계 정의 및 관리(Entity Framework)