Procedura: eseguire la logica di business quando vengono modificate le associazioni
L'esempio incluso in questo argomento è basato sul modello Sales di AdventureWorks. Per eseguire il codice incluso in questo argomento, è necessario avere già aggiunto il modello Sales di AdventureWorks al progetto e avere configurato il progetto per l'utilizzo di Entity Framework. Per ulteriori informazioni, vedere Procedura: utilizzare la Procedura guidata Entity Data Model (Entity Framework) o Procedura: configurare manualmente un progetto di Entity Framework e Procedura: definire manualmente un modello EDM (Entity Framework).
Esempio
Address che rappresenta l'indirizzo di spedizione.SalesOrderHeader e anche il gestore per questo evento è implementato in questa classe parziale.
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"); } } }
Vedere anche
Attività
Procedura: eseguire la logica di business al momento della modifica dello stato dell'oggetto (Entity Framework)Procedura: eseguire la logica di business quando vengono modificate le proprietà scalari (Entity Framework)
Procedura: eseguire la logica di business al momento del salvataggio delle modifiche (Entity Framework)