Поделиться через


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).

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:

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).

In This Section

See Also

Other Resources

Object Services (Entity Framework)
Working with Custom Objects (Entity Framework Tasks)