Entity Types in Entity Framework

Entity Types in Entity Framework

In this article, I will discuss the Entity Types in Entity Framework. In Entity Framework, there are two types of entities that allow developers to use their own custom data classes together with the data model without making any modifications to the data classes themselves.

The two types of Entities are

  1. POCO entities
  2. Dynamic Proxy entities
POCO Entities

POCO stands for “plain-old” CLR objects which can be used as existing domain objects with your data model.

The POCO data classes which are mapped to entities are defined in a data model.

A POCO entity is a class that doesn’t depend on any framework-specific base class. It is like any other normal .NET CLR class, which is why it is called “Plain Old CLR Objects”.

The POCO entities are supported in both EF 6 and EF Core.

These POCO entities support most of the same query, insert, update, and delete behaviors as entity types that are generated by the Entity Data Model. The following is an example of an Employee POCO entity.

public class Employee
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Gender { get; set; }
    public Nullable <int> Salary { get; set; }
    public Nullable <int> DepartmentId { get; set; }
    public virtual Department Department { get; set; }
}
Dynamic Proxy Entities 

The Dynamic Proxy is a runtime proxy class which wraps POCO entity. Dynamic proxy entities allow lazy loading.

Note: Dynamic proxy entities are only supported in EF 6. EF Core 2.0 does not support them yet.

  1. A POCO entity should meet the following requirements to become a POCO proxy:
  2. The POCO class must be declared with public access.
  3. A POCO class must not be sealed.
  4. The POCO class must not be abstract.
  5. Each navigation property must be declared as public, virtual.
  6. All the collection property must be ICollection<T>.
  7. The ProxyCreationEnabled option must NOT be false (default is true) in context class.

The following POCO entity meets all of the above requirements to become a dynamic proxy entity at runtime.

public class Employee
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string Gender { get; set; }
    public Nullable <int> Salary { get; set; }
    public Nullable <int> DepartmentId { get; set; }
    public virtual Department Department { get; set; }
}
Get the Entity Type from the Proxy Type:

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.

namespace EFDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            using (EF_Demo_DBEntities DBEntities = new EF_Demo_DBEntities())
            {
                var employee = DBEntities.Employees.Find(1);
                var employeeType = ObjectContext.GetObjectType(employee.GetType());       
                Console.WriteLine("FullName of Employee Master Type :" + employeeType.FullName);
                Console.ReadKey();
            }
        }
    }
}

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.

Note: By default, the dynamic proxy is enabled for every entity. However, you can disable dynamic proxy in the context by setting this.Configuration.ProxyCreationEnabled = false; as shown below

namespace EFDemo
{
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;
    public partial class EF_Demo_DBEntities : DbContext
    {
        public EF_Demo_DBEntities()
            : base("name=EF_Demo_DBEntities")
        {
            this.Configuration.ProxyCreationEnabled = false;
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
        
        public virtual DbSet<Department> Departments { get; set; }
        public virtual DbSet<Employee> Employees { get; set; }
    }
}

In the next article, I will discuss the Entity States in Entity Framework.

SUMMARY

In this article, I try to explain Entity Types in Entity Framework. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article

Leave a Reply

Your email address will not be published. Required fields are marked *