To edit or delete data, update or remove entity objects from a context and call the SaveChanges()
method. EF API will build the appropriate UPDATE or DELETE command and execute it to the database.How Entity Framework Works?
Here, you will see an overview of how entity framework works.
Entity Framework API (EF6 & EF Core) includes the ability to map domain (entity) classes to the database schema, translate & execute LINQ queries to SQL, track changes occurred on entities during their lifetime, and save changes to the database.
Entity Data Model
The very first task of EF API is to build an Entity Data Model (EDM). EDM is an in-memory representation of the entire metadata: conceptual model, storage model, and mapping between them.
Conceptual Model: EF builds the conceptual model from your domain classes, context class, default conventions followed in your domain classes, and configurations.
Storage Model: EF builds the storage model for the underlying database schema. In the code-first approach, this will be inferred from the conceptual model. In the database-first approach, this will be inferred from the targeted database.
Mappings: EF includes mapping information on how the conceptual model maps to the database schema (storage model).
EF performs CRUD operations using this EDM. It uses EDM in building SQL queries from LINQ queries, building INSERT, UPDATE, and DELETE commands, and transform database result into entity objects.
Querying
EF API translates LINQ-to-Entities queries to SQL queries for relational databases using EDM and also converts results back to entity objects.
Saving
EF API infers INSERT, UPDATE, and DELETE commands based on the state of entities when the SaveChanges()
method is called. The ChangeTrack keeps track of the states of each entity as and when an action is performed.
Entity Framework Architecture
The following figure shows the overall architecture of the Entity Framework.
Let's look at the components of the architecture individually.
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: The 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 (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: Entity SQL is another query language (For EF 6 only) 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 returning 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 LINQ-to-Entities 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.
ADO.Net Data Provider: This layer communicates with the database using standard ADO.Net.
Development Approaches with Entity Framework
There are three different approaches you can use while developing your application using Entity Framework:
- Database-First
- Code-First
- Model-First
Database-First Approach
In the database-first development approach, you generate the context and entities for the existing database using EDM wizard integrated into Visual Studio or executing EF commands.
EF 6 supports the database-first approach extensively.
Code-First Approach
Use this approach when you do not have an existing database for your application. In the code-first approach, you start writing your entities (domain classes) and context class first and then create the database from these classes using migration commands.
Developers who follow the Domain-Driven Design (DDD) principles, prefer to begin with coding their domain classes first and then generate the database required to persist their data.
Model-First Approach
In the model-first approach, you create entities, relationships, and inheritance hierarchies directly on the visual designer integrated in Visual Studio and then generate entities, the context class, and the database script from your visual model.
Choosing the Development Approach for Your Application
Use the following flow chart to decide which is the right approach to develop your application using Entity Framework:
As per the above figure, if you already have an existing application with domain classes, then you can use the code-first approach because you can create a database from your existing classes. If you have an existing database, then you can create an EDM from an existing database in the database-first approach. If you do not have an existing database or domain classes, and you prefer to design your DB model on the visual designer, then go for the Model-first approach.