Entity Framework Architecture
In this article, I am going to discuss the Entity Framework Architecture in Detail. Please read our previous article where we discussed the Overview of the Entity Framework. At the end of this article, you will understand the following pointers in detail.
- What is the Entity Framework?
- The Architecture of Entity Framework.
- Understanding the different components of Entity Framework.
- How does Entity Framework work?
What is the Entity Framework?
As we already discussed in our previous article, the Entity Framework is an Object-Relational Mapper (O/RM) that enables .NET developers to work with a database using .NET objects. It eliminates the need for most of the data-access code that developers usually need to write to perform the CRUD operations.
Entity Framework Architecture
The Architecture of the Entity Framework is composed of the following components
- The Entity Data Model
- LINQ to Entities
- Entity SQL
- The Object Services Layer
- Entity Client Data Provider
- ADO.NET Data Provider
The following diagram shows the overall architecture of the Entity Framework.
Let’s discuss each of the components of the Entity Framework Architecture in detail.
EDM (Entity Data Model):
The Entity Data Model (EDM) abstracts the logical or the relational schema and exposes the conceptual schema of the data using a three-layered approach i.e.
- The Conceptual Model (C- Space),
- Mapping model (C-S Space)
- Storage model (S – Space)
The conceptual model contains the model classes (i.e. entities) and their relationships. This will be independent of your database table design. It defines your business objects and their relationships in XML files.
A Mapping Model consists of information about how the conceptual model is mapped to the storage model. The Mapping model is responsible for mapping the conceptual and logical layers (Storage layer). It maps the business objects and the relationships defined in the conceptual layer with the tables and relationships defined in the logical layer.
Storage Model / Logical Model:
The storage model represents the schema of the underlying database. That means the storage model is the database design model which includes tables, views, Keys, stored procedures, and their relationships. The Entity Data Model uses the following three types of XML files to represent the C-Space, C-S Space, and the S-Space respectively.
- .CSDL (Conceptual Schema Definition Language): This represents the C-S Space and is used to map the entity types used in the conceptual model.
- .MSL (Mapping Schema Language): This represents the C-S Space and is used to map the logical model to the conceptual model.
- .SSDL (Store Schema Definition Language): This represents the S-Space and is used to map the schema information of the logical layer.
LINQ to Entities:
LINQ-to-Entities (L2E) 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 is another query language (For EF 6 only) just like LINQ to Entities. However, it is a little more difficult than LINQ-to-Entities (L2E) and the developer will have to learn it separately. These E-SQL queries are internally translated to data store-dependent SQL queries. The conversion of the E-SQL queries to their datastore-specific query language like T-SQL is handled by the Entity Framework.
In a real-time scenario, most of the time we might have to work with entities such as in-memory objects or a collection of in-memory objects. To do this we need Object Services. We can use it to query data, from almost all data stores, with less code.
The Object Services layer is the Object Context, which represents the session of interaction between the applications and the data source.
- The main use of the Object Context is to perform different operations like add, update and delete instances of entities and to save the changed state back to the database with the help of queries.
- It is the ORM layer of Entity Framework, which represents the data result to the object instances of entities.
- This service allows the developer to use some of the rich ORM features like primary key mapping, change tracking, etc. by writing queries using LINQ and Entity SQL.
Apart from this, the Object Services Layer provides the following additional services:
- Change tracking
- Lazy loading
- Optimistic concurrency
- Merging data
- Identity resolution
- Support for querying data using Entity SQL and LINQ to Entities
We will discuss the Object services Layer in detail, in our upcoming articles.
Entity Client Data Provider:
The main responsibility of this layer is to convert LINQ-to-Entities or Entity SQL queries into a SQL query that is understood by the underlying database. It communicates with the ADO.Net data provider which in turn sends or retrieves data from the database.
ADO.Net Data Provider:
This layer communicates with the database using standard ADO.Net.
In the next article, I am going to discuss the Context Class in Entity Framework with an example. Here, in this article, I try to explain the Entity Framework Architecture in Detail. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.
About the Author:
Pranaya Rout has published more than 3,000 articles in his 11-year career. Pranaya Rout has very good experience with Microsoft Technologies, Including C#, VB, ASP.NET MVC, ASP.NET Web API, EF, EF Core, ADO.NET, LINQ, SQL Server, MYSQL, Oracle, ASP.NET Core, Cloud Computing, Microservices, Design Patterns and still learning new technologies.