Types of Entity in Entity Framework:

There was four types of Entities in Entity Framework 4.x, 1) EntityObject 2) POCO 3) POCO Proxy (dnamic proxy) 4) Self-Tracking Entities.

 

Entity Framework 5.0 is limited to POCO entity and Dynamic proxy entity. EntityObject used to use with ObjectContext which is not being used in Entity Framework 5.0. Also, it is not recommended to use self-tracking entities.

 

POCO (Plain Old CLR Object): POCO class is the class that doesn’t depends 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 (also known as persistence-ignorant objects) support most of the same query, insert, update, and delete behaviors as entity types that are generated by the Entity Data Model. Eg. Following is a Student POCO class.

                    	  
    public class Student
    {
        public int StudentID { get; set; }

        public string StudentName{ get; set; }

        public Standard Standard{ get ; set; }

        public StudentAddress StudentAddress{ get ; set; }

        public IList<Course> Courses{ get; set; }

    }

Dynamic Proxy (POCO Proxy):

 

Dynamic Proxy is a runtime proxy class of POCO entity. POCO entity becomes POCO Proxy entity if it meets the certain requirements to enable lazy loading and automatic change tracking. It adds some methods at runtime to your POCO class which does instant change tracking and lazy loading stuff.

 

POCO entity should meet the following requirement to become POCO proxy:
  1. A custom data class must be declared with public access.
  2. A custom data class must not be sealed (NotInheritable in Visual Basic)
  3. A custom data class must not be abstract (MustInherit in Visual Basic).
  4. ProxyCreationEnabled option should not set to false (default is true) in context class
  5. Each navigation property must be declared as public, virtual

 

Note: By default dynamic proxy is enabled for every entity. However, you can disable dynamic proxy by setting ProxyCreationEnabled option to false in context class.
context.Configuration.ProxyCreationEnabled = false;

 

EDM generates POCO entities which satisfy above requirement of dynamic proxy by default.

 

Following is Student POCO class generated by EDM:

    
    public partial class Student
    {
        public Student()
        {
            this.Courses = new HashSet<Course>();
        }
    
        public int StudentID { get; set; }
        public string StudentName { get; set; }
        public Nullable<int> StandardId { get; set; }
    
        public  Standard Standard { get; set; }
        public StudentAddress StudentAddress { get; set; }
        public virtual ICollection<Course> Courses { get; set; }
    }

At runtime type of Student will be System.Data.Entity.DynamicProxies.Student as below:

 

Entity Framework 5.0 Tutorial

 

Getting the actual entity type from a dynamic proxy:

 

You can use ObjectContext.GetObjectType() to find actual type of dynamic proxy as below:

 

Entity Framework 5.0 Tutorial

 

Entity can have two types of properties, Scalar properties and Navigation properties.

 

Scalar properties are properties whose actual values are contained in the entity. For example, Student entity has scalar properties e.g. StudentId, StudentName. These corresponds with the Student table columns.

 

Navigation properties are pointers to other related entities. The Student has Standard property as navigation property that will enable application to navigate from a Student to related Standard entity.