- 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;
-
-
- }
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 instanceWhen 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 typeA 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());
-
-
- Console.WriteLine("FullName of Employee Master Type :" + emType.FullName);
- }
OutputThe 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.
SummaryThis 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.