[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]
There are cases when 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 that the application will 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 the Entity Framework. You might also want to have more control over your entity types than you would have if you extended the generated partial classes. The Entity Framework enables you to use custom data classes by either inheriting from EntityObject and ComplexObject or by implementing a set of interfaces. For more information, see Interfaces and Inheritance for Custom Objects (Entity Framework). Changing your custom data classes in this manner enables your application to take advantage of the same core entity object functionality as code-generated entity types. However, not all data classes can be modified for use with the Entity Framework. For example, when domain objects are defined outside the scope of an application, you may not be able to modify them for use with an external persistence framework.
The Entity Framework enables you to use custom data classes together with your data model without making any modifications to the data classes themselves. This means that you can use "plain-old" CLR objects (POCO), such as existing domain objects, with your data model. These persistence-ignorant data classes, which are mapped to entities defined in a data model, support most of the same query, insert, update, and delete behaviors as entity types that are generated by the Entity Data Model tools.
Proxy Object Creation
You can use persistence-ignorant classes in your Entity Framework applications along or along with proxy classes. Including proxy classes allows the Entity Framework to add change tracking and lazy loading functionality to your persistence-ignorant classes. These proxy classes derive from your persistence-ignorant classes and implement the interfaces required by the Entity Framework for change tracking and relationship management. To include proxy classes, your custom data classes must meet the requirements that are described in Requirements for Using Persistence-Ignorant Objects (Entity Framework).
If you choose not to enable proxy classes along with your persistence-ignorant classes, you must manage your own change tracking. You can detect changes by comparing a snapshot of current values with the initial snapshot taken when objects are loaded into the object context. For more information, see Tracking Changes in Persistence-Ignorant Objects (Entity Framework).
By default, proxy objects are serialized along with persistence-ignorant objects. We recommend that you disable proxies before serializing your persistence-ignorant objects to another tier. To disable proxy class generation, set the value of the ProxyCreationEnabled property to false on the instance of ObjectContextOptions that is returned by the ContextOptions property on the ObjectContext, as in the following example.
// Disable proxy object creation.
context.ContextOptions.ProxyCreationEnabled = false;
With binary serialization and data contract serialization, related objects are serialized together with the primary object. Lazy loading performs a query for each relationship navigation property that is accessed, and both binary and WCF data contract serializers will access all relationship navigation properties. This can cause many unexpected queries to be performed during serialization. If you did not disable proxy type generation (which would disable lazy loading), disable lazy loading explicitly.
context.ContextOptions.LazyLoadingEnabled = false;
If your custom data classes do not meet the additional requirements for proxy type creation, proxy creation will be automatically disabled. Additional requirement for enabling proxy creation is you need to create your object using the CreateObject method on the ObjectContext instead of the new operator.
Even when proxy creation is disabled, persistence-ignorant objects can be attached to the object context without the equivalent proxy object. A combination of persistence-ignorant objects with and without associated proxy objects is also supported. For more information, see How to: Create a Persistence-Ignorant Object (Entity Framework).
In This Section
See Also