How to: Define a Custom Object Context
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.
Note: |
|---|
| 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
-
Add a class code file to your project.
-
Include the following namespaces:
System
System.Data.Objects
System.Collections.Generic
-
Rename the class to POCOAdventureWorksEntities. Make sure the class inherits from the ObjectContext class.
-
Define member variables of the ObjectSet type for each POCO entity type:
-
Define constructors to the POCOAdventureWorksEntities class.
-
Define properties that return
ObjectSetobjects.
Example
This example shows the custom object context code that supports the Contact, Order, and LineItem custom data classes.
Note: