- POCO class is the class that doesn’t depend on any framework specific base class.
- It is like any other normal .NET class that is why it is called “Plain Old CLR Objects”.
- These POCO entities (Persistence-ignorant objects) support most of the behaviors as entity types that are generated by the Entity Data Model.
- Entity can have two types of properties: Scalar properties and Navigation properties.
- Scalar properties are properties whose actual values are contained in the entity.
- Navigation properties are pointers to other related entities.
Dynamic Proxy (POCO Proxy)
- Dynamic Proxy is a runtime proxy class of POCO entity.
- "POCO entity" becomes "POCO Proxy entity" if it meets certain requirements.
- POCO Proxy adds some methods at runtime to the POCO class which will help with change tracking and lazy loading.
- The proxy overrides some virtual properties of the entity to insert hooks for performing actions automatically when the property is accessed.
- POCO entity should meet the following requirements to become a POCO proxy:
- A custom data class must be
public
.
- A custom data class must not be
sealed
.
- A custom data class must not be
abstract
.
ProxyCreationEnabled
option should not set to false (default is true) in context class.
- Each navigation property must be
public
and virtual
.
- By default dynamic proxy is enabled for every entity.
- You can disable dynamic proxy by setting the
ProxyCreationEnabled
option to false in context class.
context.Configuration.ProxyCreationEnabled = false;
Explicitly Creating an Instance of a Proxy
- A proxy instance will not be created if you create an instance of an entity using the new operator.
- If you need to create a proxy instance (for example, so that lazy loading or proxy change tracking will work) then you can do so using the
Create
method.
- Note that the
Create
method does not add or attach the created entity to the context.
using (var context = new BloggingContext())
{
var blog = context.Blogs.Create();
// The generic version of Create can be used if you want to create an instance of a derived entity type.
var admin = context.Users.Create<Administrator>();
}
Getting the Actual Entity Type From a Proxy Type
- EDM generates POCO entities which satisfy the above requirements for a dynamic proxy by default.
- At runtime type of
YourClass
will be System.Data.Entity.DynamicProxies.YourClass
.