Working With Proxies in Entity Framework

Introduction

When creating an instance of a POCO entity type, the Entity Framework creates an instance of a dynamically generated derived type that acts as a proxy for this entity. A POCO entity has some virtual properties, this proxy overrides these virtual properties of this entity to perform an action automatically when the property is accessed. This type of mechanism is used to support lay loading and the change tracking of the entity.

A POCO proxy is a runtime proxy class of the POCO entity. If we check the type of the entity at runtime then it has a long name like: System.Data.Entity.DynamicProxies.EmployeeMaster_260ECB16225F4E5E0232BA72326492E9BADA61D68BE4DCEF48D365296F69975F

proxy

Runtime Proxy

Generally we don't need to understand what proxy classes are and what is the use of them. But there are some exceptions in which we should understand what proxy classes are.

  • In some scenarios, we want to serialize the data of the entity, not the proxy classes. In this case we can prevent the Entity Framework from creating a proxy instance.
  • In some cases, we want to create an instance of the entity class, not the proxy class (we don't require lazy loading and change track type of functionality).
  • An actual entity might be required to get from the proxy type. In this case we can use the static method "GetObjectType" of ObjectContext.

Disabling proxy creation

Sometimes it is good to disable creation of a proxy instance. Proxy creation can be disabled by using the ProxyCreationEnabled flag. In the following example I have put it in the constructor of the context.

With DbContext

  1. public partial class TestdbEntities : DbContext  
  2. {  
  3.     public TestdbEntities() : base("name=TestdbEntities")  
  4.     {  
  5.         this.Configuration.ProxyCreationEnabled = false;  
  6.     }  
  7.       
  8.     protected override void OnModelCreating(DbModelBuilder modelBuilder)  
  9.     {  
  10.         throw new UnintentionalCodeFirstException();  
  11.     }  
  12.       
  13.     public virtual DbSet<DepartmentMaster> DepartmentMasters { getset; }  
  14.     public virtual DbSet<EmployeeDetail> EmployeeDetails { getset; }  
  15.     public virtual DbSet<EmployeeMaster> EmployeeMasters { getset; }  

With ObjectContext

  1. public partial class TestdbEntities : ObjectContext  
  2. {  
  3.     public TestdbEntities() : base("name=TestdbEntities""TestdbEntities")  
  4.     {  
  5.         this.ContextOptions. ProxyCreationEnabled= false;  
  6.         OnContextCreated();  
  7.     }  
  8. }    
We can also disable creation of a proxy at the time of object creation of the context instead of disabling it at the constructor of the context.
  1. using (var Context = new TestdbEntities())  
  2. {  
  3.    Context.Configuration.ProxyCreationEnabled = false;  
  4.    //Use following code for EF 4.0  
  5.    // Context. ContextOptions.ProxyCreationEnabled  = false;  

context

Entity Framework does not create proxie instances for the type where there is nothing to do with a proxy. It means that we can avoid creating proxies for the type that are sealed or that have no virtual properties.

Explicitly creating a proxy instance

When the flag ProxyCreationEnabled is set to false, the proxy instance will not be created with creating a new instance of an entity. This might not be a problem but we can create a proxy instance using the Create method of DbSet.
  1. using (var Context = new TestdbEntities())  
  2. {  
  3.     var employee = Context.EmployeeMasters.Create();  

The Create method has an overloaded version that accepts a generic type. This can be used to create an instance of a derived type.
  1. using (var Context = new TestdbEntities())  
  2. {  
  3.     var employee = Context.EmployeeMasters.Create<Staff>();  

The Create method just creates the instance of the entity type if the proxy type for the entity would have no value (it is nothing to do with a proxy). The Create method does not add or attach the entity with the context object.

Get the Entity type from the proxy type

A proxy type has a long name like :

System.Data.Entity.DynamicProxies.EmployeeMaster_260ECB16225F4E5E0232BA72326492E9BADA61D68BE4DCEF48D365296F69975F

We can find the original or actual entity type from this proxy type using the GetObjectType method of ObjectContext. This is a static method so there is no need to create an object of the object context.
  1. using (var Context = new TestdbEntities())  
  2. {  
  3.     var em = Context.EmployeeMasters.Find(1);  
  4.     var emType = System.Data.Entity.Core.Objects. ObjectContext.GetObjectType(em.GetType());  
  5.     //Use following code for EF 4.0  
  6.    //var emType = System.Data.Objects.ObjectContext.GetObjectType(em.GetType());  
  7.     Console.WriteLine("FullName of Employee Master Type :" + emType.FullName);  

Output

Output

The GetObjectType returns an actual entity type even if we pass the instance of entity type (not the proxy type). In short we can always use this method to get the actual entity type, there is no need to check whether the type is a proxy type or not.

Summary

This article has helped you to understand what a proxy in the Entity Framework is and how to turn on and turn off a proxy and how to create a proxy instance explicitly.

Up Next
    Ebook Download
    View all
    Learn
    View all