방법: 스칼라 속성 변경 중 비즈니스 논리 실행(Entity Framework)

Entity Framework 를 사용하면 생성된 속성이 변경될 때 자체 사용자 지정 비즈니스 논리를 실행하여 사용자 지정 작업을 수행할 수 있습니다. 엔터티 데이터 모델 도구는 개념적 모델의 엔터티를 나타내는 데이터 클래스를 생성합니다. 이렇게 생성된 클래스를 직접 수정할 수는 없지만, 생성된 속성별로 이 도구는 OnPropertyChangingOnPropertyChanged라는 이름의 부분 메서드(Partial Method) 쌍도 생성합니다. 여기서 Property는 속성의 이름입니다. 이러한 메서드는 속성 변경 전후에 Entity Framework 에 의해 호출되며, partial 데이터 클래스의 이러한 메서드를 확장하여 사용자 지정 코드를 구현할 수 있습니다.

Cc716747.note(ko-kr,VS.100).gif참고:
개체 계층 코드가 기본 텍스트 템플릿 이외의 텍스트 템플릿을 사용하여 생성된 경우에는 부분 메서드(Partial Method)가 생성되지 않았을 수 있습니다.

이 항목의 예제는 AdventureWorks Sales 모델을 기반으로 합니다. 이 예제의 코드를 실행하려면 프로젝트에 AdventureWorks Sales 모델을 추가하고 Entity Framework를 사용하도록 프로젝트를 구성해야 합니다. 이렇게 하려면 방법: Entity Framework 프로젝트 수동 구성방법: 수동으로 모델 및 매핑 파일 정의(Entity Framework)의 절차를 수행합니다.

속성 변경의 사용자 지정 유효성 검사를 구현하려면

  1. 프로젝트에서 유효성을 검사할 데이터 클래스별 사용자 지정 partial 클래스를 정의합니다.

  2. 이 partial 클래스에서 다음 메서드를 하나 또는 두 가지 모두 정의합니다. 여기서 Property는 유효성을 검사할 속성의 이름입니다.

    • On 속성 Changing - 속성 유효성 검사와 같이 변경이 발생하기 전에 실행할 코드를 포함합니다. value 매개 변수는 속성이 변경되는 기준 값입니다. 속성 변경이 발생하기 전에 그 유효성을 검사하도록 이 메서드를 구현합니다. 변경이 수행되지 않게 하려면 예외를 throw해야 합니다.

    • On 속성 Changed - 변경 기록과 같이 변경이 발생한 후 실행할 코드를 포함합니다.

예제

이 예제에서는 SalesOrderDetail.OrderQty가 변경되기 전에 주문 변경이 가능한지 그리고 보류 중인 변경이 파일에 기록되어 있는지 확인하기 위해 SalesOrderHeader.Status의 값을 확인합니다. 이 작업은 OnOrderQtyChanging 부분 메서드(Partial Method)에서 수행됩니다. 변경을 수행할 수 없는 경우 예외가 발생합니다. 그런 다음 변경이 성공적으로 수행되면 SalesOrderHeader.Status 값은 1로 재설정되며 완료된 변경이 기록됩니다. 이 작업은 OnOrderQtyChanged 부분 메서드(Partial Method)에서 수행됩니다.

Partial Public Class SalesOrderDetail
    Inherits EntityObject
    Private Sub OnOrderQtyChanging(ByVal value As Short)
        ' Only handle this change for existing SalesOrderHeader 
        ' objects that are attached to an object context. If the item 
        ' is detached then we cannot access or load the related order. 
        If EntityState <> EntityState.Detached Then
            Try
                ' Ensure that the referenced SalesOrderHeader is loaded. 
                If Not Me.SalesOrderHeaderReference.IsLoaded Then
                    Me.SalesOrderHeaderReference.Load()
                End If

                ' Cancel the change if the order cannot be modified. 
                If Me.SalesOrderHeader.Status > 3 Then
                    Throw New InvalidOperationException("The quantity cannot be changed " & _
                                                        "or the item cannot be added because the order has either " & _
                                                        "already been shipped or has been cancelled.")
                End If

                ' Log the pending order change. 
                File.AppendAllText(LogFile, "Quantity of item '" & _
                    Me.SalesOrderDetailID.ToString() & "' in order '" & _
                    Me.SalesOrderHeader.SalesOrderID.ToString() & _
                    "' changing from '" + Me.OrderQty.ToString() & _
                    "' to '" & value.ToString() + "'." & Environment.NewLine & _
                    "Change made by user: " & Environment.UserName & _
                    Environment.NewLine)
            Catch ex As InvalidOperationException
                Throw New InvalidOperationException(("The quantity could not be changed " & " because the order information could not be retrieved. " & "The following error occurred:") + ex.Message)
            End Try
        End If
    End Sub
    Private Sub OnOrderQtyChanged()
        ' Only handle this change for existing SalesOrderHeader 
        ' objects that are attached to an object context. 
        If EntityState <> EntityState.Detached Then
            Try
                ' Ensure that the SalesOrderDetail is loaded. 
                If Not SalesOrderHeaderReference.IsLoaded Then
                    SalesOrderHeaderReference.Load()
                End If

                ' Reset the status for the order related to this item. 
                Me.SalesOrderHeader.Status = 1

                ' Log the completed order change. 
                File.AppendAllText(LogFile, "Quantity of item '" & _
                    SalesOrderDetailID.ToString() + "' in order '" & _
                    SalesOrderHeader.SalesOrderID.ToString() & _
                    "' successfully changed to '" + OrderQty.ToString() & _
                    "'." + Environment.NewLine & _
                    "Change made by user: " + Environment.UserName & _
                    Environment.NewLine)
            Catch ex As InvalidOperationException
                Throw New InvalidOperationException(("An error occurred; " & _
                                                     "the data could be in an inconsistent state. ") & _
                                                 Environment.NewLine + ex.Message)
            End Try
        End If
    End Sub
End Class
public partial class SalesOrderDetail : EntityObject
{
    partial void OnOrderQtyChanging(short value)
    {
        // Only handle this change for existing SalesOrderHeader 
        // objects that are attached to an object context. If the item
        // is detached then we cannot access or load the related order.
        if (EntityState != EntityState.Detached)
        {
            try
            {
                // Ensure that the referenced SalesOrderHeader is loaded.
                if (!this.SalesOrderHeaderReference.IsLoaded)
                {
                    this.SalesOrderHeaderReference.Load();
                }

                // Cancel the change if the order cannot be modified.
                if (this.SalesOrderHeader.Status > 3)
                {
                    throw new InvalidOperationException("The quantity cannot be changed "
                    + "or the item cannot be added because the order has either "
                    + "already been shipped or has been cancelled.");
                }

                // Log the pending order change.
                File.AppendAllText(LogFile, "Quantity of item '"
                    + this.SalesOrderDetailID.ToString() + "' in order '"
                    + this.SalesOrderHeader.SalesOrderID.ToString()
                    + "' changing from '" + this.OrderQty.ToString()
                    + "' to '" + value.ToString() + "'." + Environment.NewLine
                    + "Change made by user: " + Environment.UserName
                    + Environment.NewLine);
            }
            catch (InvalidOperationException ex)
            {
                throw new InvalidOperationException("The quantity could not be changed "
                + " because the order information could not be retrieved. "
                + "The following error occurred:" + ex.Message);
            }
        }
    }
    partial void OnOrderQtyChanged()
    {
        // Only handle this change for existing SalesOrderHeader 
        // objects that are attached to an object context.
        if (EntityState != EntityState.Detached)
        {
            try
            {
                // Ensure that the SalesOrderDetail is loaded.
                if (!SalesOrderHeaderReference.IsLoaded)
                {
                    SalesOrderHeaderReference.Load();
                }

                // Reset the status for the order related to this item.
                this.SalesOrderHeader.Status = 1;

                // Log the completed order change.
                File.AppendAllText(LogFile, "Quantity of item '"
                    + SalesOrderDetailID.ToString() + "' in order '"
                    + SalesOrderHeader.SalesOrderID.ToString()
                    + "' successfully changed to '" + OrderQty.ToString()
                    + "'." + Environment.NewLine
                    + "Change made by user: " + Environment.UserName
                    + Environment.NewLine);
            }
            catch (InvalidOperationException ex)
            {
                throw new InvalidOperationException("An error occurred; "
                + "the data could be in an inconsistent state. "
                + Environment.NewLine + ex.Message);
            }
        }
    }
}

참고 항목

작업

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

기타 리소스

Entity Data Model Tools