Entity Framework (EF) is an object-relational mapper for .NET developers that allows them to work with relational data using domain-specific objects. It eliminates the majority of the data-access code that developers must routinely write.
Developers may work with data in domain-specific objects and properties, such as customers and customer addresses, without worrying about the underlying database tables and columns where this data is kept, courtesy of the Entity Framework.
Entity Framework is used in the following scenarios.
- Create an MVC web app.
- Set up the site style.
- Install Entity Framework 6.
- Create the data model.
- Create the database context.
- Initialize DB with test data.
- Set up EF 6 to use LocalDB.
- Create controller and views
*Features of C# Entity Framework *
- Entity Framework is a lightweight and extensible object-relational mapping (ORM) technology. According to the individual needs of each project, developers can add or delete specific components from the ORM system.
- Entity Framework supports multiple platforms like Windows, Linux, and macOS.
- Entity Framework supports both relational and non-relational data sources. Entity Framework works efficiently with widely used databases like SQL Server, SQL Server Compact, SQLite, PostgreSQL, Azure Table Storage, and IBM Data Server.
- Entity Framework makes it easier for programmers to perform create, read, update and delete (CRUD) operations by supporting databases. It also makes it easier for developers to perform unit testing by keeping in-memory tables.
- C# Entity Framework allows us to configure the Entity Framework model through data annotation properties or the Fluent API, to alter default conventions.
- C# Entity Framework provides a series of migration commands that can be run from the NuGet Package Manager Console or the Command Line Interface. To create or manage underlying database Schema
Entity Framework Development Approaches
1. Code First Approach
This approach first targets a database that does not exist and then creates it. It allows the developers to define and make new models with C# and .NET classes. In this approach, you can use empty databases and add tables too.
2. Model First Approach
This model is best suitable for new projects where the database does not exist. This model is stored using the EDMX file and can be viewed and edited by the developer.
3. Database First Approach
This approach is an alternative for the code-first approach and the model-first approach. It creates the model and codes from the database in the project and connects them with the database and developer.
Basic Workflow in Entity Framework
- First, you need to define the model. Defining the model consists of domain classes, context classes derived from DbContext, and configuration.
- To insert data, you need to add a domain object for a context and call the savechanges() method. You need to use the insert command and execute it to the database.
- For reading data, executing the LINQ-to-Entities query in your preferred language like C# or .NET will be useful. EF API will convert the query to the SQL query, which will be provided to the database for execution.
- For editing, updating, deleting, and removing entities objects, you should call the savechanges() method. EF API will build and execute the commands in the database.
Entity Framework Architecture
The Architecture of Entity Framework consists 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 Entity Data Model
The entity data model (EDM) is made up of three parts:
Conceptual Model: The conceptual model represents the model classes (also known as entities) and their relationships. This will be independent of the database table's architecture. It describes your business objects and their relationships in XML files.
Mapping Model: A mapping model specifies how the conceptual model is translated into a storage model. The Mapping model maps the conceptual and logical layers (storage layer). It integrates the business objects and relationships in the abstract layer with the tables and relationships in the logical layer.
Storage Model: The storage model represents the schema of the underlying database. The database design model contains tables, views, keys, stored procedures, and their relationships.
LINQ to Entities
Queries against the object model are written using the LINQ-to-Entities (L2E) query language. The entities defined in the conceptual model are returned.
Entity SQL
Internally, these E-SQL queries are converted to SQL queries that are data store dependent. E-SQL queries are converted to datastore-specific query languages like T-SQL via the Entity Framework (Transact SQL).
The Object Services Layer
The Object Services layer includes the Object Context, representing the interaction session between the applications and the data source. The fundamental purpose of the Object Context is to conduct various operations, such as adding and deleting instances of things and querying the database to save the modified state.
Entity Client Data provider
The Object Services layer includes the Object Context, representing the interaction session between the applications and the data source. The fundamental purpose of the Object Context is to conduct various operations, such as adding and deleting instances of things and querying the database to save the modified state.
ADO.Net Data Provider
These source-specific providers abstract the ADO.NET interfaces used to connect to the database while programming against the conceptual schema. It uses a command tree to translate standard SQL languages like LINQ into native SQL expressions, which it then executes on the given DBMS system.

Top comments (0)