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

public partial class TestdbEntities : DbContext  
{  
    public TestdbEntities() : base("name=TestdbEntities")  
    {  
        this.Configuration.ProxyCreationEnabled = false;  
    }  
      
    protected override void OnModelCreating(DbModelBuilder modelBuilder)  
    {  
        throw new UnintentionalCodeFirstException();  
    }  
      
    public virtual DbSet<DepartmentMaster> DepartmentMasters { get; set; }  
    public virtual DbSet<EmployeeDetail> EmployeeDetails { get; set; }  
    public virtual DbSet<EmployeeMaster> EmployeeMasters { get; set; }  
} 

With ObjectContext

public partial class TestdbEntities : ObjectContext  
{  
    public TestdbEntities() : base("name=TestdbEntities", "TestdbEntities")  
    {  
        this.ContextOptions. ProxyCreationEnabled= false;  
        OnContextCreated();  
    }  
}

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.

using (var Context = new TestdbEntities())  
{  
   Context.Configuration.ProxyCreationEnabled = false;  
   //Use following code for EF 4.0  
   // 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.

using (var Context = new TestdbEntities())  
{  
    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. 

using (var Context = new TestdbEntities())  
{  
    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.

using (var Context = new TestdbEntities())  
{  
    var em = Context.EmployeeMasters.Find(1);  
    var emType = System.Data.Entity.Core.Objects. ObjectContext.GetObjectType(em.GetType());  
    //Use following code for EF 4.0  
   //var emType = System.Data.Objects.ObjectContext.GetObjectType(em.GetType());  
    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.


Similar Articles