Interview Key Points About Entity Framework In A Nutshell

In this article, I am going to summarize all the import points about entity framework. No code, only points. Let’s start.

Entity Framework

  • Entity Framework is an open source ORM framework.
  • Code-First, Database-First and Model-First approaches are supported in Entity Framework

    • Code First Approach
      It enables developers to describe a model by using C# or VB.NET classes and then create database objects from these classes. Such classes are called POCO classes.

    • Model First approach
      Developers will define Entities and relationships on the design surface of the empty model (.edmx file) using VS. After designing the entities and their relationships, developers create SQL scripts and run it SQL side to create a database from it.

    • Database First Approach
      It enables developers to create a model from an existing database (like SQL Server, MySQL, Oracle, DB2 etc.).

  • SaveChanges() method of DbContext is used to save entities to the database
  • LINQ-to-Entity, Entity SQL and Native SQL query syntax can be used to query in EF 6
  • Entity Data Model (EDM) is an in-memory representation of the entire metadata: conceptual model, storage model, and mapping between them.
  • Entity Framework supports automatic change tracking of all the loaded entities through context class.
  • Database.BeginTransaction() and DbContext.Database.UseTransaction() methods can be used for transactions in EF 6.


  • POCO is Plain Old CLR Object.
  • In other words, they don’t have to derive from anything, implement anything, or reference anything except normal CLR stuff—they are plain old CLR objects.
  • Proxy Object: An object that is created from a POCO class or entities generated by the Entity Framework to support change tracking and lazy loading.
  • Proxy Class Object Rules: The class must be public and not sealed. Each property must be marked as virtual.Each property must have a public getter and setter. Any collection navigation properties must be typed as ICollection<T>.
  • Disable POCO Proxy creation using DBContext
    1. dbContext.Configuration.ProxyCreationEnabled = false;  

Entity Framework Performance

  • Lazy Loading, Entity Self Tracking etc. will impact EF performance.
  • For improving EF performance, disable Lazy Loading and Entity Self Tracking behaviours when you are loading or modifying a large number of records.
  • Use the compiled query whenever required
  • Also, for manipulating complex operations use stored procedures instead of LINQ to Entity queries and avoid using views.
  • Connected Scenario is when the Entity is retrieved from the database and some operations like modification is done in the same context.
  • Disconnected Scenario is when the Entity is retrieved from the database and some operations like modification is done in the different context. In this scenario context does not understand the changes done so we have to specify the changes done outside of context.

DB Context and ObjectContext

  • DB Context acts like a bridge between the database and entity classes.
  • DBContext will be responsible for Insert, Update and Delete functionalities of the entities.
  • DbContext can be used for DataBase first, code first and model first development.
  • DbContext is conceptually similar to ObjectContext.
  • DbContext is nothing but a ObjectContext wrapper,(i.e. lightweight alternative to the ObjectContext).
  • DbContext support Code First approach and ObjectContext does not allow Code First approach.
  • DbContext also has some sets of methods that are not available with ObjectContext, like Dbset.Find, DbSet.Local etc.
  • Get a reference to the ObjectContext from the DbContext:
    1. var objectContext = ((IObjectContextAdapter)myDbContextObject).ObjectContext;  
  • Executes a raw SQL query to the database in EF 6
    1. dbContext.Database.SqlQuery("Select * from Student where studentid=1")  
  • DbSet is a class which represents an entity set.

Lazy, Eager and Explicit Loading

  • Lazy Loading is the default behavior of Entity Framework. The dependent/related entities are loaded once they are accessed for the first time.
  • Lazy loading is good when relations are not too much.
  • Disable Lazy loading using DBContext
    1. dBContext.Configuration.LazyLoadingEnabled = false;  
  • Eager Loading will load the dependent/related entities at once.
  • Eager loading in EF 6 is achieved using Include() method.
  • Explicit Loading - Load the dependent/related entities by using Load() method when the Lazy loading disable.

Entity data model (EDM) or .edmx file

  • An Entity Data Model (EDM) acts as a bridge between application and database.
  • It enables us to work with the conceptual view of data rather than the actual database schema.
  • An .edmx file is an XML based file that includes information about your database schema.
  • This file contains mainly three parts: Conceptual Model (Conceptual schema definition language (CSDL)), Storage Model (Store schema definition language (SSDL)), and the Mapping (Mapping specification language (MSL)) between these two models.
  • You can edit/modify EDM or .edmx file.

    • CSDL
      It describes the model classes and their relationships.

    • SSDL
      It describes the database schema which includes tables, views, stored procedures and their relationships and keys.

    • MSL
      It describes how the conceptual model is mapped to the storage model.

Object State Manager

  • ObjectStateManager is responsible for tracking changes in an entity.
  • It maintains object state and tracks modifications to the object when a user performs insert, delete, or update operation on that object.
  • Entity Framework supports automatic change tracking of all the loaded entities through context class.
  • If Entity Framework needs to handle Change Tracking, each entity should have Entity Key (Primary Key).

Entity States/Cycle in EF

Added, Modified, Deleted, Unchanged, Detached are various Entity States.
  • Added
    The entity is ready to add but after the changes are saved, the object state changes to unchanged.

  • Modified 
    The entity is ready for the modification but after the changes are saved, the object state changes to unchanged.

  • Deleted 
    The entity is marked for deletion but after the changes are saved, the object state changes to Detached.

  • Unchanged 
    The entity is unmodified since it was attached to the context or when the last changes are saved.

  • Detached 
    Object to be detached. Only the entity is removed; (i.e. remove it from the object context by calling the detach() method.)

POCO VS Entity Framework Generated Classes

  • EntityObject class inherit from EntityObject class. POCO class does not inherit from any class.
  • EntityObject class created automatically when Entity Data Model is created. POCO class created manually or using templates
  • EntityObject class - Not possible to do Unit Tests. POCO class - Unit tests are possible
  • EntityObject class - True Domain Mode implementation is not possible. POCO class - True Domain Mode implementation possible
  • EntityObject class does not facilitate the separation of concerns. POCO class facilitate separation of concerns.
  • EntityObject class - The EF generated classes automatically tracks the changes in entities only if entities are tracked by the ObjectContext, automatic
  • Two-way relationship fix-ups and lazy loading. POCO class - POCO class tracking changes in entities, two-way relationship fix-ups and lazy loading will not happen automatically in POCOs

Other Key Points

  • Entity client (System.Data.EntityClient) is a data provider used by the Entity Framework to access data. It contains classes such as EntityConnection, EntityCommand, and EntityDataReader which are used to work with Entity SQL to query the database.
  • Entity Type specifies the structure of data within the EDM conceptual model. It includes a key, a set of properties to define data and navigation properties to define a relationship between two entities. It must have a unique name.
  • An Entity Containern is a logical grouping of entity sets, association sets, and function imports. There should be at least one entity container within each conceptual model and must have a unique name.
  • An entity set is defined inside an entity container. It is a logical container for instances of an entity type or instances of any type derived from that entity type.The relationship between an entity type and an entity set looks like to the relationship between a row and a table. Like a row, an entity type describes data structure and like a table, an entity set contains instances of a given structure.
  • An association set is a logical container for association instances of the same type. It is defined inside an entity container as shown in the above example.
  • An Entity Key is a property or a set of properties of an entity type that are used to verify the uniqueness of a given entity. Normally, an entity key is mapped to a primary Key or unique Key field in the database.
  • Association specifies a relationship between two entities within the entity data model -> conceptual model by the "Association" Element.

    • Types of Association
      Foreign key association and Independent association.

    • Foreign Key Association
      When the relationship between two entities is defined by the foreign key property

    • Independent Association
      There is no foreign key property defined within storage model, which  means there is no physical foreign key in the database. The relationship between two entities is defined by an independent object and handled by the object state manager.

  • A scalar property is a property of an entity type that contains the value and is mapped to a database field.
  • A navigation property is the property of an entity type that allows navigating from one entity to the other entity, doesn’t carry data and is automatically created from the primary and foreign key references.
  • T4 Template (Text Template Transformation Toolkit) will generate the C# code based on edmx XML file. (.tt extension)

Hope you enjoyed this article. Happy coding……..