Customizing Objects (Entity Framework)
The ADO.NET Entity Framework provides tools to automatically generate an object layer based on the conceptual schema definition language (CSDL) file of an Entity Data Model (EDM). These data classes can be customized to varying degrees, depending on the requirements of your application. You can also modify your own custom data classes to use them with an EDM. This is useful when you upgrade data classes from an existing application to use the Entity Framework, or when you want more control of how data classes are created.
Extending Partial Data Classes
Types that are defined in an EDM do not have associated methods like the classes used in object-oriented programming. Instead, they only contain properties that are defined in the EDM. You can add functionality to objects by extending the generated partial data classes. When data classes are generated by the Entity Data Model tools, they are implemented in partial classes. A partial class splits the definition of a class over two or more source files. Each source file contains a section of the class definition, and all sections are combined when the application is compiled. For more information, see Partial (Visual Basic) or partial (Type) (C# Reference).
Having partial classes enables you to extend these classes with custom methods and properties in a separate source file without having to worry about losing your customization when the generated files are refreshed. For more information, see How to: Customize Generated Data Objects (Entity Framework).
Custom Business Logic
When using generated data classes, you can invoke custom business logic during certain Object Services operations, such as changes to properties or relationships. This business logic might include creating additional validation or logging when changing properties or calling SaveChanges. You can invoke custom business logic by handling events raised by Object Services or by defining custom partial methods that are called when properties are changed.
The following are the events and methods that are used to invoke custom business logic:
- On Property Changing and OnPropertyChanged partial methods
A pair of partial methods on generated data classes that are called by Object Services when a property is changed. Extend these methods in partial data classes to implement code that is executed when a property changes. For more information, see How to: Execute Business Logic During Property Changes (Entity Framework).
- SavingChanges Event
An event that is raised when changes cached in the object context are about to be saved to the data source. Handle this event to implement custom business logic at the start of a SaveChanges operation. For more information, see How to: Execute Business Logic When Saving Changes (Entity Framework).
- 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 (Entity Framework).
- AssociationChanged Event
An event that is raised when a relationship between two objects is changed. Handle this event to implement custom business logic when a relationship is changed. For more information, see How to: Change Relationships Between Objects (Entity Framework).
Custom Data Classes
There are cases when only extending partial classes by adding methods and properties might not offer enough flexibility. Consider an existing .NET Framework application that uses ADO.NET to load data from a database into CLR objects for the application to use. These objects might contain valuable customizations and business logic that must be preserved in the object layer. These customizations might prevent you from migrating the application to using the Entity Framework. You might also want to have more control over your entity types than merely extending the generated partial classes.
The recommended way to use custom data classes with an EDM is to inherit from EntityObject. If you cannot inherit from EntityObject, or if you need more independence from the framework, the Entity Framework provides a set of interfaces that you can implement to use custom data classes with an EDM. For more information, see Implementing Custom Data Class Interfaces (Entity Framework). When you use custom data classes, you must apply attributes to custom data classes and properties and notify the object context when changing properties.
Inheriting from EntityObject
Generated data classes inherit from EntityObject or ComplexObject. If you have to use custom data classes with an EDM, the recommended way is to modify your data classes to inherit from one of these two base classes. In this manner, custom data classes benefit from the change tracking and relationship management functionality that is provided by EntityObject.
When you inherit from EntityObject and ComplexObject, be aware of the following points:
Class and property names must match the entity type and property names that are defined in the conceptual schema definition language (CSDL) file of the EDM. If the names do not match, you must either change the names of your data classes and properties or update the names in the CSDL and the associated mapping specification file (MSL). For more information, see How to: Customize an Entity Data Model to Work with Custom Objects (Entity Framework).
For every property on the entity type that is defined in the CSDL, there must be an associated property in the custom data class. A custom data class can have additional properties that do not map to properties that are defined in the CSDL.
You must apply attributes to custom data classes and properties. These attributes provide an explicit mapping between the objects and the CSDL. For more information, see Object-Entity Mapping Attributes (Entity Framework) and How to: Map Custom Objects to Entities (Entity Framework).
To enable Object Services to track changes to custom objects, you must report these changes in a specific pattern by using the EntityObject change reporting methods. For more information, see Reporting Changes in Custom Data Classes (Entity Framework).
Entity Framework tools also generate additional classes that simplify object programming. It is beneficial to use this code with custom data classes. For more information, see How to: Use Object Services with Custom Objects (Entity Framework).
When you inherit from EntityObject, complex types must be implemented by inheriting from ComplexObject. For more information, see Complex Type Objects (Entity Framework).
The following example defines the custom data class Order, which inherits from EntityObject:
<EdmEntityTypeAttribute(NamespaceName:="Microsoft.Samples.Entity", Name:="Order")> _ Public Class Order Inherits EntityObject
[EdmEntityTypeAttribute(NamespaceName="Microsoft.Samples.Entity",Name="Order")] public class Order : EntityObject
For more information, see How to: Inherit from the EntityObject and ComplexObject Base Classes (Entity Framework).