Share via


方法: アソシエーションの変更時にビジネス ロジックを実行する

このトピックでは、エンティティ間のアソシエーションが変わるときにビジネス ロジックを実行する方法について説明します。

このトピックの例には、Adventure Works Sales Model が使用されています。このトピックのコードを実行するには、あらかじめプロジェクトに Adventure Works Sales Model を追加し、Entity Framework を使用するようにプロジェクトを構成しておく必要があります。詳細については、「Entity Data Model ウィザードを使用する方法 (Entity Framework)」、または「Entity Framework プロジェクトを手動で構成する方法」、および「Entity Data Model を手動で定義する方法 (Entity Framework)」を参照してください。

また、次の using ステートメント (Visual Basic の場合は Imports) をコードに追加する必要があります。

Imports System.ComponentModel
using System.ComponentModel;

この例は、「EntityReference を使用してオブジェクト間のリレーションシップを変更する方法 (Entity Framework)」のトピックの例を展開したものです。 この例では、出荷先住所を表す Address オブジェクトの EntityReferenceAssociationChanged イベントを処理することによって出荷先住所が変更された場合に注文状態を確認する方法を示します。 注文状態が 3 より大きい場合、その注文は変更できずに例外が発生します。 SalesOrderHeader 部分クラスのコンストラクターでデリゲートが定義され、このイベントのハンドラーもこの部分クラスで実装されます。 これにより、特定の注文で出荷先住所が変更されるたびに注文の状態が確認されるようになります。

また、OnPropertyChanging メソッドと OnPropertyChanged メソッドをこのイベントで呼び出して、PropertyChanging イベントと PropertyChanged イベントをそれぞれ発生させることもできます。 これらのイベントはクライアント コントロールにアソシエーションの変更を通知します。

SalesOrderHeaderAddress のリレーションシップの他方の End で変更を検証するには、同様の手法を使用して、出荷先住所に関連付けられている SalesOrderHeader オブジェクトの EntityCollectionAssociationChanged イベントを登録します。

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)