Implementing Custom Data Class Interfaces (Entity Framework)

.NET Framework 3.5

The recommended way to use custom data classes with an Entity Data Model (EDM) is to inherit from EntityObject and ComplexObject. For cases when you cannot inherit from EntityObject and ComplexObject or when you need a higher degree of independence from the framework, the Entity Framework provides a set of custom data class interfaces. If you do not inherit from EntityObject, you must implement these interfaces to use custom data classes with an EDM. The specific interfaces that you implement depend on the requirements of your custom data classes and your application.

IEntityWithChangeTracker

Required for change tracking. Enables Object Services to track changes to the object.

Object Services provides objects with the IEntityChangeTracker interface to enable objects to report changes. IEntityWithChangeTracker defines the SetChangeTracker method. This method specifies the IEntityChangeTracker used to report changes. For more information, see Reporting Changes in Custom Data Classes (Entity Framework).

IEntityWithKey

Optional. Exposes the entity key to Object Services for improved performance.

IEntityWithKey defines the EntityKey property. Object Services uses the EntityKey property to manage objects in the object context.

If you choose not to implement IEntityWithKey, you will see decreased performance and increased memory usage when loading related objects, attaching objects to an object context, or any operation that requires a key.

IEntityWithRelationships

Required for entities with associations. Enables Object Services to manage relationships between objects.

IEntityWithRelationships defines the RelationshipManager property. Object Services uses the RelationshipManager property to access the RelationshipManager used to manage relationships to other objects.

For more information, see How to: Implement Custom Data Class Interfaces (Entity Framework).

Like custom data classes that inherit from EntityObject, classes that implement these interfaces must meet the following requirements:

  • There must be an object for each entity type that is defined in the conceptual schema definition language (CSDL) file.

  • The namespace, classes, and data properties must have the appropriate EDM attributes applied.

  • The names of the namespace, classes, and data properties that have EDM attributes applied must match the names in the corresponding CSDL file.

For more information, see Customizing Objects (Entity Framework).

The following example shows the code that is required to implement these interfaces for an Order object, where Order is based on the SalesOrderHeader table.

No code example is currently available or this language may not be supported.

Complex Types

Complex types are non-scalar properties of entity types that enable scalar properties to be organized within entities. For more information, see Complex Type (EDM). Tracking changes in complex type objects requires you to write custom change tracking code. Therefore, we recommend inheriting from EntityObject and ComplexObject when possible. There are no custom data class interfaces to implement for complex type objects. However, you can use the following procedure to implement change tracking with complex type objects that do not inherit from ComplexObject.

NoteNote

If you choose to implement custom data class interfaces for objects but also to inherit from ComplexObject, you must still implement custom change tracking as described in the following procedure.

To implement change tracking for complex type objects

  1. Implement custom data interfaces for entity types. For more information, see How to: Implement Custom Data Class Interfaces (Entity Framework).

  2. Ensure that complex types are correctly defined in the conceptual and mapping sections of the EDM. For more information, see Complex Type (EDM).

  3. Define an abstract base class called ComplexTypeChangeTracker.

    No code example is currently available or this language may not be supported.
  4. Define the complex type class, which inherits from ComplexTypeChangeTracker, and apply the EdmComplexTypeAttribute.

    No code example is currently available or this language may not be supported.
  5. In the complex type class, override the SetComplexChangeTracker method.

    No code example is currently available or this language may not be supported.
  6. Implement standard change tracking on the scalar properties of the complex type. For more information, see Reporting Changes in Custom Data Classes (Entity Framework).

  7. Apply the EdmComplexPropertyAttribute to the complex property in the entity type, and add a call to SetComplexChangeTracker to reset the change tracker when the complex property changes.

    No code example is currently available or this language may not be supported.
  8. Repeat steps 4-7 for each complex property.

  9. In the System.Data.Objects.DataClasses.IEntityWithChangeTracker.SetChangeTracker(System.Data.Objects.DataClasses.IEntityChangeTracker) implementation for the entity type, insert a call to SetComplexChangeTracker to set the change tracker. Do this once for each complex property in the type.

    No code example is currently available or this language may not be supported.

See Also

Community Additions

Show: