Types of Entity in Entity Framework

There are two types of Entities in Entity Framework 5.0/6.0: POCO entity and dynamic proxy entity.

  1. POCO Entity (Plain Old CLR Object)
    1. POCO class is the class that doesn’t depend on any framework specific base class. It is like any other normal .net class which is why it is called “Plain Old CLR Objects”.
    2. 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.
  2. Dynamic Proxy (POCO Proxy)
    1. Dynamic Proxy is a runtime proxy class of POCO entity. It is like a wrapper class of POCO entity. Dynamic proxy entities allow lazy loading and automatic change tracking.
    2. 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)
  • A POCO class must not be abstract (MustInherit in Visual Basic).
  1. Each navigation property must be declared as public, virtual
  2. Each collection property must be ICollection<T>
  3. ProxyCreationEnabled option must NOT be false (default is true) in context class

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:

ef5

Scalar properties:

Scalar properties are properties whose actual values are contained in the entity. For example, Student entity has scalar properties like StudentId and 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 the application to navigate from a Student to related Standard entity.

What is meant by DBContext???

Prior to EntityFramework 4.1, EDM used to generate context classes that were derived from the ObjectContext class. It was a little tricky to work with ObjectContext. DbContext is conceptually similar to ObjectContext. It is a wrapper around ObjectContext which is useful in all the development models: Code First, Model First and Database First.

DbContext is an important part of Entity Framework. It is a bridge between your domain or entity classes and the database.

ef4

DbContext is the primary class that is responsible for interacting with data as object. DbContext is responsible for the following activities:

  • EntitySet: DbContext contains entity set (DbSet<TEntity>) for all the entities which is mapped to DB tables.
  • Querying: DbContext converts LINQ-to-Entities queries to SQL query and send it to the database.
  • Change Tracking: It keeps track of changes that occurred in the entities after it has been querying from the database.
  • Persisting Data: It also performs the Insert, Update and Delete operations to the database, based on what the entity states.
  • Caching: DbContext does first level caching by default. It stores the entities which have been retrieved during the life time of a context class.
  • Manage Relationship: DbContext also manages relationship using CSDL, MSL and SSDL in DB-First or Model-First approach or using fluent API in Code-First approach.

Object Materialization: DbContext converts raw table data into entity objects.

Entity Framework Architecture

ef3

  • EDM (Entity Data Model): EDM consists of three main parts – Conceptual model, Mapping and Storage model.
  • Conceptual Model: The conceptual model contains the model classes and their relationships. This will be independent from your database table design.
  • Storage Model: Storage model is the database design model which includes tables, views, stored procedures, and their relationships and keys.
  • Mapping: Mapping consists of information about how the conceptual model is mapped to the storage model.
  • LINQ to Entities: LINQ to Entities is a query language used to write queries against the object model. It returns entities, which are defined in the conceptual model. You can use your LINQ skills here.
  • Entity SQL: Entity SQL is another query language just like LINQ to Entities. However, it is a little more difficult than L2E and the developer will have to learn it separately.
  • Object Service:Object service is a main entry point for accessing data from the database and to return it back. Object service is responsible for materialization, which is the process of converting data returned from an entity client data provider (next layer) to an entity object structure.
  • Entity Client Data Provider:The main responsibility of this layer is to convert L2E or Entity SQL queries into a SQL query which is understood by the underlying database. It communicates with the ADO.Net data provider which in turn sends or retrieves data from the database.
  • Net Data Provider:This layer communicates with the database using standard ADO.Net.

What is O/RM?

ORM is a tool for storing data from domain objects to relational database like MS SQL Server, in an automated way, without much programming. O/RM includes three main parts: Domain class objects, Relational database objects and Mapping information on how domain objects map to relational database objects (tables, views & storedprocedures). ORM allows us to keep our database design separate from our domain class design. This makes the application maintainable and extendable. It also automates standard CRUD operation (Create, Read, Update & Delete) so that the developer doesn’t need to write it manually.

A typical ORM tool generates classes for the database interaction for your application as shown below

ef2

Entity framework introduction

Writing and managing ADO.Net code for data access is a tedious and monotonous job. Microsoft has provided an O/RM framework called “Entity Framework” to automate database related activities for your application.

Entity framework is an Object/Relational Mapping (O/RM) framework. It is an enhancement to ADO.NET that gives developers an automated mechanism for accessing & storing the data in the database.

Entity framework is useful in three scenarios. First, if you already have existing database or you want to design your database ahead of other parts of the application. Second, you want to focus on your domain classes and then create the database from your domain classes. Third, you want to design your database schema on the visual designer and then create the database and classes.

ef1