Entity Types in Entity Framework


There are two types of entities in Entity Framework, that allow engineers to use custom data classes togather with data model without any modifications to the data classes.

   1. POCO entities
   2. Dynamic Proxy

POCO Entity (Plain Old CLR Object):

POCO stands for "plain-old" CLR objects which can be used as existing domain objects with data model. 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".

POCO entities, also known as persistence ignorant objects, support most of the same queries, insert, update, and delete behaviors as entity types that are generated by the EDM (Entity Data Model).


Dynamic Proxy (POCO Proxy):

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

To become a POCO proxy, POCO entity should meet the followings:-

  •     A POCO class must be declared with public access modifier.
  •     A POCO class must not be sealed (NotInheritable in Visual Basic)
  •     A POCO class must not be abstract (MustInherit in Visual Basic).
  •     Each navigation property must be declared as public, virtual
  •     Each collection property must be ICollection<T>
  •     In context class, ProxyCreationEnabled option must be true (default is true).
  •     The class cannot implement the IEntityWithChangeTracker or IEntityWithRelationships interfaces because the proxy classes implement these interfaces.

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

Disabling the dynamic proxy
context.Configuration.ProxyCreationEnabled = false;

 

ObjectContext.GetObjectType() can be used to find the actual type of dynamic proxy.

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

Scalar Properties:-

Scalar properties are properties whose actual values are contained in the entity. For example, Employees entity has scalar properties like EmpId and EmpName. These are the columns in Employee table.


Navigation Properties:-

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

 

Praesent mattis

Pellentesque viverra vulputate enim. Aliquam erat volutpat. Pellentesque tristique ante ut risus. Quisque dictum. Integer nisl risus, sagittis convallis, rutrum id, elementum congue, nibh. Suspendisse dictum porta lectus. Donec placerat odio vel elit.

Read More