Aracılığıyla paylaş


Defining Business Logic

Add business logic to your Entity Framework application by handling the events that are fired during certain operations, such as changes to properties or relationships. This business logic might include creating additional validation or logging when properties change or when the SaveChanges method is called. You can invoke custom business logic by handling events raised by the Entity Framework or by defining custom partial methods that are called when properties are changed. This topic lists some events and provides links to other related topics. 

Event and Partial Methods Description

System.Data.Objects.ObjectContext.ObjectMaterialized

Raised when a new entity is created from data in the data source as part of a query or load operation. The event occurs after all scalar, complex, and reference properties have been set on an object, but before collections are loaded. If an object with the same key value exists in the object context, the Entity Framework will not recreate the object and this event will not be raised.

System.Data.Objects.ObjectContext.SavingChanges

Raised at the start of a SaveChanges operation on an ObjectContext. This event is typically used to validate changed objects before new values are written to the database.

For more information, see How to: Execute Business Logic When Saving Changes.

System.Data.Objects.ObjectStateManager.ObjectStateManagerChanged

Occurs when entities leave the context through delete or detach methods or enter the context through queries or add and attach methods. For more information, see How to: Execute Business Logic When the Object State Changes.

Dd456876.note(en-us,VS.100).gifNote:
If you are using POCO entities without change tracking proxies, the change is detected when you call the DetectChanges method or the SaveChanges method, which calls DetectChanges.

System.Data.Objects.DataClasses.RelatedEnd.AssociationChanged

Occurs when an association changes. If you are using the default code generated entities or POCO entities with proxy objects, then this occurs at the same time the change is made to the actual navigation property:

Dd456876.note(en-us,VS.100).gifNote:
If you are using POCO entities without proxies, the change is detected when you call the DetectChanges method or the SaveChanges method, which calls DetectChanges.

Events may be suppressed during bulk operations, such as Load, Attach, and Clear.

For more information, see How to: Execute Business Logic During Association Changes.

System.Data.Objects.DataClasses.StructuralObject.PropertyChanging

Occurs when a scalar property value change is a pending. The Entity Framework generated classes call ReportPropertyChanging in the scalar property setters. The ReportPropertyChanging method raises the PropertyChanging event. Handle this event to execute business logic, such as validation, before a property value is changed. For more information, see How to: Execute Business Logic During Scalar Property Changes.

When tracking changes that are made to custom data classes that implement IEntityWithChangeTracker, you must use IEntityChangeTracker to report property changes.

Dd456876.note(en-us,VS.100).gifNote:
To be notified when the navigation property changes, register for the System.Data.Objects.DataClasses.RelatedEnd.AssociationChanged event. For more information, see How to: Execute Business Logic During Association Changes.

System.Data.Objects.DataClasses.StructuralObject.PropertyChanged

Occurs when a scalar property value has changed. The Entity Framework generated classes call ReportPropertyChanged in the scalar property setters. The ReportPropertyChanged method raises the PropertyChanged event. Handle this event to execute business logic, such as writing to a log, when a property is changed. For more information, see How to: Execute Business Logic During Scalar Property Changes.

When tracking changes that are made to custom data classes that implement IEntityWithChangeTracker, you must use IEntityChangeTracker to report property changes.

Dd456876.note(en-us,VS.100).gifNote:
To be notified when the navigation property changes, register for the System.Data.Objects.DataClasses.RelatedEnd.AssociationChanged event. For more information, see How to: Execute Business Logic During Association Changes.

OnContextCreated Method

The Entity Data Model tools generate an OnContextCreated partial method in the class that represents the EntityContainer for the model and that inherits from the ObjectContext class. This partial method is called whenever an ObjectContext is instantiated. Implement this partial method in your code to register a handler for the SavingChanges event. For more information, see How to: Execute Business Logic When Saving Changes.

OnPropertyNameChanging and OnPropertyNameChanged

The Entity Data Model tools generate partial methods in the scalar property set accessors for each entity type. The OnPropertyNameChanging partial method is called before the value is set. The OnPropertyNameChanged partial method is called after the value is set. Implement these partial methods in to add business logic.

See Also

Concepts

Customizing Objects

Other Resources

Entity Data Model Tools