How to: Define a Custom Object Context

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

When you use POCO entities, you disable object layer generation by the Entity Framework. In addition to defining custom entity types, you need to either define a custom object context type or manage your own connections through a manually created instance of EntityConnection that you pass to the constructor of the ObjectContext. For information about how to create an EntityConnection, see How to: Build an EntityConnection Connection String

This topic demonstrates how to create a custom object context.

To disable object layer generation with the Entity Data Model Designer (Entity Designer), open the .edmx file in the Entity Designer. Right-click on the designer surface and select Properties. In the Properties window, select the Code Generation Strategy property and select None.

The custom object context class manages the POCO entities that are defined in How to: Define POCO Entities.

A custom object context should include the following functionality:

  • The ability to instantiate an ObjectContext that is specific to your conceptual model, including predefined connections.

  • Properties that return type-specific ObjectSet objects.

For information about defining custom entity types, see How to: Define POCO Entities.

To use generated object context code in your custom object context code

  1. Add a class code file to your project.

  2. Include the following namespaces:




  3. Rename the class to POCOAdventureWorksEntities. Make sure the class inherits from the ObjectContext class.

  4. Define member variables of the ObjectSet type for each POCO entity type:

  5. Define constructors to the POCOAdventureWorksEntities class.

  6. Define properties that return ObjectSet objects.


This example shows the custom object context code that supports the Contact, Order, and LineItem custom data classes.

See Also