Aracılığıyla paylaş


Requirements for Creating POCO Proxies

The Entity Framework supports POCO classes ("plain-old" CLR objects). If you want to enable lazy loading for POCO entities and to have the Entity Framework track changes in your classes as the changes occur, your POCO classes must meet the requirements described in this topic so that the Entity Framework can create proxies for your POCO entities during run time. The proxy classes derive from your POCO types.

Class Definition Requirements

The Entity Framework creates proxies for POCO entities if the classes meet the requirements described below. POCO entities can have proxy objects that support change tracking or lazy loading. You can have lazy loading proxies without meeting the requirements for change tracking proxies, but if you meet the change tracking proxy requirements, then the lazy loading proxy will be created as well. You can disable lazy loading by setting the LazyLoadingEnabled option to false.

For either of these proxies to be created:

  • A custom data class must be declared with public access.

  • A custom data class must not be sealed (NotInheritable in Visual Basic)

  • A custom data class must not be abstract (MustInherit in Visual Basic).

  • A custom data class must have a public or protected constructor that does not have parameters. Use a protected constructor without parameters if you want the CreateObject method to be used to create a proxy for the POCO entity. Calling the CreateObject method does not guarantee the creation of the proxy: the POCO class must follow the other requirements that are described in this topic.

  • The class cannot implement the IEntityWithChangeTracker or IEntityWithRelationships interfaces because the proxy classes implement these interfaces.

  • The ProxyCreationEnabled option must be set to true.

For lazy loading proxies:

  • Each navigation property must be declared as public, virtual (Overridable in Visual Basic), and not sealed (NotOverridable in Visual Basic) get accessor. The navigation property defined in the custom data class must have a corresponding navigation property in the conceptual model. For more information, see Loading Related POCO Entities.

For change tracking proxies:

  • Each property that is mapped to a property of an entity type in the data model must have non-sealed (NotOverridable in Visual Basic), public, and virtual (Overridable in Visual Basic) get and set accessors.

  • A navigation property that represents the "many" end of a relationship must return a type that implements ICollection, where T is the type of the object at the other end of the relationship.

  • If you want the proxy type to be created along with your object, use the CreateObject method on the ObjectContext when creating a new object, instead of the new operator.

If during run time you need to know whether a POCO entity is a proxy, follow the steps described in How to: Identify that a POCO Entity is a Proxy.

See Also

Tasks

Walkthrough: Serialize POCO Proxies with WCF

Concepts

Working with POCO Entities
Tracking Changes in POCO Entities