방법: 연결 변경 중 비즈니스 논리 실행

이 항목에서는 엔터티 간 연결이 변경될 때 비즈니스 논리를 실행하는 방법을 보여 줍니다.

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

또한 다음 using 문(Visual Basic에서는 Imports)을 코드에 추가해야 합니다.

Imports System.ComponentModel
using System.ComponentModel;

예제

이 예제는 방법: EntityReference를 사용하여 개체 간 관계 변경(Entity Framework) 항목의 예제를 확장하며, 배송 주소가 변경될 때 배송 주소를 나타내는 Address 개체에 대해 EntityReference에서 AssociationChanged 이벤트를 처리하여 주문 상태를 확인하는 방법을 보여 줍니다. 주문 상태가 3보다 클 경우 해당 주문은 변경할 수 없으며 예외가 발생합니다. SalesOrderHeader partial 클래스의 생성자에서 위임이 정의되며, 이 이벤트의 처리기 역시 이 partial 클래스에서 구현됩니다. 따라서 주문의 배송 주소가 바뀔 때마다 주문 상태가 확인됩니다.

이 이벤트에서 OnPropertyChangingOnPropertyChanged 메서드를 호출하여 PropertyChangingPropertyChanged 이벤트를 각각 발생시킬 수도 있습니다. 이러한 이벤트는 클라이언트 컨트롤에 연결 변경을 알립니다.

SalesOrderHeader-Address 관계의 반대 쪽에서 변경의 유효성을 검사하려면 비슷한 방법으로 배송 주소와 관련된 SalesOrderHeader 개체의 EntityCollection에서 AssociationChanged 이벤트를 등록할 수 있습니다.

Partial Public Class SalesOrderHeader
    ' SalesOrderHeader default constructor. 
    Public Sub New()
        ' Register the handler for changes to the 
        ' shipping address (Address1) reference. 
        AddHandler Me.AddressReference.AssociationChanged, AddressOf ShippingAddress_Changed
    End Sub

    ' AssociationChanged handler for the relationship 
    ' between the order and the shipping address. 
    Private Sub ShippingAddress_Changed(ByVal sender As Object, ByVal e As CollectionChangeEventArgs)
        ' Check for a related reference being removed. 
        If e.Action = CollectionChangeAction.Remove Then
            ' Check the order status and raise an exception if 
            ' the order can no longer be changed. 
            If Me.Status > 3 Then
                Throw New InvalidOperationException("The shipping address cannot " & _
                                                    "be changed because the order has either " & _
                                                    "already been shipped or has been cancelled.")
            End If
            ' Call the OnPropertyChanging method to raise the PropertyChanging event. 
            ' This event notifies client controls that the association is changing. 
            Me.OnPropertyChanging("Address1")
        ElseIf e.Action = CollectionChangeAction.Add Then
            ' Call the OnPropertyChanged method to raise the PropertyChanged event. 
            ' This event notifies client controls that the association has changed. 
            Me.OnPropertyChanged("Address1")
        End If
    End Sub
End Class
public partial class SalesOrderHeader
{
    // SalesOrderHeader default constructor.
    public SalesOrderHeader()
    {
        // Register the handler for changes to the 
        // shipping address (Address1) reference.
        this.AddressReference.AssociationChanged
            += new CollectionChangeEventHandler(ShippingAddress_Changed);
    }

    // AssociationChanged handler for the relationship 
    // between the order and the shipping address.
    private void ShippingAddress_Changed(object sender,
        CollectionChangeEventArgs e)
    {
        // Check for a related reference being removed. 
        if (e.Action == CollectionChangeAction.Remove)
        {
            // Check the order status and raise an exception if 
            // the order can no longer be changed.
            if (this.Status > 3)
            {
                throw new InvalidOperationException(
                    "The shipping address cannot "
                + "be changed because the order has either "
                + "already been shipped or has been cancelled.");
            }
            // Call the OnPropertyChanging method to raise the PropertyChanging event.
            // This event notifies client controls that the association is changing.
            this.OnPropertyChanging("Address1");
        }
        else if (e.Action == CollectionChangeAction.Add)
        {
            // Call the OnPropertyChanged method to raise the PropertyChanged event.
            // This event notifies client controls that the association has changed.
            this.OnPropertyChanged("Address1");
        }
    }
}

참고 항목

작업

방법: 개체 상태가 변경될 때 비즈니스 논리 실행(Entity Framework)
방법: 스칼라 속성 변경 중 비즈니스 논리 실행(Entity Framework)
방법: 변경된 내용을 저장할 때 비즈니스 논리 실행(Entity Framework)