HOW TO:在關聯變更期間執行商務邏輯

本主題示範如何在實體間的關聯變更時執行商務邏輯。

本主題的範例根據 Adventure Works Sales Model。若要執行此主題中的程式碼,您必須已經將 Adventure Works Sales Model 加入到專案中,並設定您的專案使用 Entity Framework。如需詳細資訊,請參閱 HOW TO:使用實體資料模型精靈 (Entity Framework)HOW TO:手動設定 Entity Framework 專案HOW TO:手動設定 Entity Framework 專案

您也必須將以下 using 陳述式 (在 Visual Basic 中為 Imports) 加入至程式碼:

Imports System.ComponentModel
using System.ComponentModel;

範例

本範例是擴充 HOW TO:使用 EntityReference 變更物件間的關聯性 (Entity Framework) 主題中的範例。 本範例示範如何在送貨地址變更時檢查訂單狀態,其方式是針對代表送貨地址的 Address 物件來處理 EntityReference 上的 AssociationChanged 事件。 如果訂單狀態大於 3,就無法變更訂單,而且會引發例外狀況。 此委派會定義在 SalesOrderHeader 部分類別的建構函式 (Constructor) 中,而且此事件的處理常式也會在這個部分類別中實作。 如此可確保每當訂單的送貨地址變更時,都會檢查訂單狀態。

您也可以在這個事件中呼叫 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");
        }
    }
}

另請參閱

工作

HOW TO:在物件狀態變更時執行商務邏輯
HOW TO:在純量屬性變更期間執行商務邏輯 (Entity Framework)
HOW TO:在儲存變更時執行商務邏輯 (Entity Framework)