ASP.NET

What is Entity Framework?

entity-framework-database-first-asp-net-core

Microsoft Entity Framework (EF) is an Object Relational Mapping framework that provides an abstraction layer (a kind of mapping) between two incompatible systems (i.e. Object oriented programming languages with Relational databases). It enables the developers to work with domain specific objects. It enables us to interact with relational data in an object oriented way, meaning we can retrieve and manipulate data as strongly typed objects using LINQ queries.

Components of Entity Framework Architecture

Below are the components of Entity Framework –

  • Entity Data Model (EDM)
  • LINQ to Entities
  • Entity SQL
  • Object Service
  • Entity Client Data Provider
  • ADO.Net Data Provider

Entity Data Model (EDM)

  • Conceptual Model : These are the model classes which contain the relationships. These are independent of the database design.
  • Mapping : The Mapping will have the information on how the Conceptual Models are mapped to Storage Models.
  • Storage Model : Storage Models are database design models, which contains database tables, views, stored procedures and keys with relationships.

LINQ to Entities

LINQ to Entities is a query language which we used to write queries against the object models and the query result will return the entities defined in the Conceptual Model.

Entity SQL

Entity SQL is a query language is like LINQ to Entities. This is a bit complex compared to LINQ to Entities. A developer who is using this should learn this separately.

Entity Client Data Provider

Responsibility of Entity Client Data Provider is to convert the LINQ to Entities or Entity SQL queries to a SQL query, which is understood by the underlying database. This finally communicates with ADO.NET Data Provider which in turn used to talk to the database.

DB Context

When we create an edmx file, it will have the list of entities and context class which will be derived from DB Context class. DBContext is responsible for Insert, Update and Delete functionalities of the entities. It acts like a bridge between the database and entity classes.

Object Context

Object Context manages all the database operations, like database connection, and manages various entities of the Entity Model. DB Context is a wrapper around Object Context.

Difference between ObjectContext and DbContext

Conceptually, both these are here for the same reason.

  • DBContext is wrapper around Object Context.
  • Prior to EF 4.1, EDM used to use Object Context as base class for Context classes.
  • There were some difficulties been observed in Object Context so now Db Context is introduced in EF 6.0 and this is used for all the development models –
  • Database First, Model First and Code First.
  • Object Context supports complied queries, but DB Context not.
  • Object Context supports self-tracking entities, but DB Context does not.
  • DB Context is thread safe, but Object Context not.