如何:在关联更改过程中执行业务逻辑

本主题介绍如何在实体之间的关联发生更改时执行业务逻辑。

本主题中的示例基于 Adventure Works 销售模型。若要运行本主题中的代码,则必须已经将 Adventure Works 销售模型添加到了您的项目中,并且已经将项目配置为使用实体框架。有关更多信息,请参见如何:使用实体数据模型向导(实体框架)如何:手动配置实体框架项目如何:手动定义实体数据模型(实体框架)

还必须向代码中添加下列 using 语句(在 Visual Basic 中为 Imports):

Imports System.ComponentModel
using System.ComponentModel;

示例

本示例对如何:使用 EntityReference 更改对象之间的关系(实体框架)主题中的示例进行了扩展。 本示例演示如何在送货地址发生更改时通过在表示送货地址的 Address 对象的 EntityReference 上处理 AssociationChanged 事件来检查订单状态。 如果订单状态大于 3,则订单无法更改,将引发异常。 构造函数中为 SalesOrderHeader 分部类定义了委托,此事件的处理程序也在该分部类中实现。 这将确保在每次订单送货地址更改时都检查订单状态。

还可以在此事件中调用 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");
        }
    }
}

另请参见

任务

如何:当对象状态发生更改时执行业务逻辑
如何:在标量属性更改过程中执行业务逻辑(实体框架)
如何:在保存更改时执行业务逻辑(实体框架)