Types of Entity in Entity Framework:

We created EDM for existing database in the previous section. AS you have seen in the previous section that EDM contains entities for each table in the database. There are two types of Entities in Entity Framework 5.0/6.0: POCO entity and dynamic proxy entity.

POCO Entity (Plain Old CLR Object):

POCO class is the class that doesn't depend 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. The following is an example of Student POCO entity.

    
        public class Student
        {
            public Student()
            {
                this.Courses = new List<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 IList<Course> Courses { get; set; }
        }
        

Dynamic Proxy (POCO Proxy):

Dynamic Proxy is a runtime proxy class of POCO entity. It is like wrapper class of POCO entity. Dynamic proxy entities allow lazy loading and automatic change tracking.

POCO entity should meet the following requirements to become a POCO proxy:

  1. A POCO class must be declared with public access.
  2. A POCO class must not be sealed (NotInheritable in Visual Basic)
  3. A POCO class must not be abstract (MustInherit in Visual Basic).
  4. Each navigation property must be declared as public, virtual
  5. Each collection property must be ICollection<T>
  6. ProxyCreationEnabled option must NOT be false (default is true) in context class

The following Student POCO entity meets all the above requirement to become dynamic proxy entity at runtime.

    
        public 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 virtual Standard Standard { get; set; }
            public virtual StudentAddress StudentAddress { get; set; }
            public virtual ICollection<Course> Courses { get; set; }
        }
        

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

context.Configuration.ProxyCreationEnabled = false;

EDM generates POCO entities which satisfy the above requirements for a dynamic proxy by default.

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

Getting the actual entity type from a dynamic proxy:

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

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

Scalar 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 correspond with the Student table columns.

Navigation properties:

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