Note: this article is published on 02/14/2022.
After I wrote several articles on this site, I found out it seemed almost for every article, I needed to set up a sample application associated with an entity framework if accessing the database. And, every time, I needed to rewrite the setup process from scratch in order for a new reader to follow along easily. Even for introducing a very simple concept, such as Caching, I needed to spend 80% of the time setting up the sample app, and only 20% on introducing the Caching concept itself.
Therefore, I think it is better to write a basic model such as entity framework sample for various approaches, and then I can reuse them when needed. I made a list of the series of articles below, I will write them one by one, while the Entity framework overview and concept will be covered in article (0):
- Entity Framework (0), Overview --- this article
- Entity Framework (1), with .Net MVC, Code-First
- Entity Framework (2), with .Net MVC, Database-First
- Entity Framework (3), with .Net MVC, Model-First
- Entity Framework (4), with .Net Core MVC, Code-First
- Entity Framework (5), with .Net Core MVC, Database-First
- Entity Framework (6), with .Net Core MVC, Model-First
- Entity Framework (7), with .Net WPF, Database-First
- Entity Framework (8), with .NET Core Web API, Stored Procedure
- Entity Framework (9), with .NET Core Web API, Stored Procedure Implementation
- Entity Framework (10), with .Net WebForms, Database-First
- Entity Framework (11), with .Net Core Razor Pages Code-First
- Entity Framework (12), with New .Net Core MVC Code-First
- Entity Framework (13), with .Net Core Code-First Summary
Note
We write the Entity Framework for MVC module, but the pattern is the same or similar when applying to Web Application or Web API.
Introduction
This article is the first article in this series, written on Mar. 23, 2021, but never finished till today. I will make it done according to the previous collecting materials, the references below, and give an overview of the Entity Framework.
The structure of this article:
- Introduction
- A - Overview of the ADO.NET Entity Framework
- What is Entity Framework?
- Why Entity Framework?
- B - Architecture of the ADO.NET Entity Framework;
- C - Approaches of the ADO.NET Entity Framework
- C-1: Code First
- C-2: Model First
- C-3: Database First
- Comparing Approaches
- Selecting Right Approach
Note: see additional info from Entity Framework (0-1), Overview Additions.
A - Overview of the ADO.NET Entity Framework
What is Entity Framework?
Entity Framework is an Object Relational Mapper (ORM) which is a type of tool that simplifies mapping between objects in your software to the tables and columns of a relational database.
Entity Framework was first released in 2008, Microsoft's primary means of interacting between .NET applications and relational databases.
- Entity Framework (EF) is an open source ORM framework for ADO.NET which is a part of .NET Framework.
- An ORM takes care of creating database connections and executing commands, as well as taking query results and automatically materializing those results as your application objects.
- An ORM also helps to keep track of changes to those objects, and when instructed, it will also persist those changes back to the database for you.
Why Entity Framework?
Entity Framework is an ORM and ORMs are aimed to increase the developer’s productivity by reducing the redundant task of persisting the data used in the applications.
- Entity Framework can generate the necessary database commands for reading or writing data in the database and execute them for you.
- Performing basic CRUD (Create, Read, Update, Delete) operations.
- Easily managing "1 to 1", "1 to many", and "many to many" relationships.
- Ability to have inheritance relationships between entities.
- Entity Framework can express your queries against your domain objects using LINQ to entities.
- Entity Framework will execute the relevant query in the database and then materialize results into instances of your domain objects for you to work within your app.
Therefore:
- Entity Framework is Microsoft's recommended data access technology for new applications.
- Entity Framework is where all of the forward moving investment is being made, which has been the case for a number of years already.
- Microsoft recommends that you use Entity Framework over ADO.NET or LINQ to SQL for all new development.
B - Architecture of the ADO.NET Entity Framework
The ADO.NET Entity Framework is built in a layered architecture on top of existing ADO.NET 2.0 data providers.
- The ADO.NET 2.0 Data Providers offer a common programming model for accessing disparate data sources.
- A connection object,
- A command object, and
- A data reader object.
- The ADO.NET Entity Framework introduces a new data provider called EntityClient; this is the first layer above the ADO.NET 2.0 data providers. With same programming abstractions:
- Connections,
- Commands, and
- Data readers, Plus adding
- Mapping capabilities that translate queries expressed in terms of the model into the equivalent queries in terms of the tables in the databases.
- Above the EntityClient layer, the ADO.NET Entity Framework includes an Object Services layer, which provides a programming model in terms of strongly-typed objects.
- Mapping entity types into data classes
The Object Services layer supports querying with both Entity SQL and Language Integrated Query (LINQ).
Developers can choose to program against either the EntityClient layer or the Object Services layer. This choice provides flexibility for different application scenarios.
A simple Architecture of the ADO.NET Entity Framework:
C - Approaches of the ADO.NET Entity Framework
The Entity Framework provides three approaches to create an entity model and each one has their own pros and cons (the contents of this part is mainly referenced from [ref] with a little editing).
- Code First
- Model First
- Database First
C-1 - Code First
The Code First approach enables us to create a model and their relation using classes and then create the database from these classes. It enables us to work with the Entity Framework in an object-oriented manner. Here we need not worry about the database structure.
Advantages
- There is full control of the model from the code. There is no EDMX/designer and no auto-generated code
- It supports database migrations, so it is very easy to sync various databases
- We have more customization options and more control
- We can also use Code First to map our model to an existing database; to learn more click here
Disadvantages
- It is very difficult to maintain a database compared to a visual design tool
- Requires good knowledge of C# and data annotation
C-2 - Model First
In this approach, model classes and their relation is created first using the ORM designer and the physical database will be generated using this model. The Model First approach means we create a diagram of the entity and relation that will be converted automatically into a code model.
Advantages
- Visual designer to create a database schema
- Model diagram can be easily updated when the database changes
- Entity Framework generates the Code and database script
- Extensibility is possible using a partial class
Disadvantages
- When we change the model and generate SQL to sync the database then this will always result in data loss because the tables are dropped first.
- We don't have much control on entities and database.
- Requires good knowledge of Entity Framework to update the model and database
C-3 - Database First
The Database First approach enables us to create an entity model from the existing database. This approach helps us to reduce the amount of code that we need to write.
Advantages
- This approach is used when we have an existing database
- The EDM wizard creates the entities and their relationships automatically
- Entity model is created automatically from Database.
- Easy update if any changes are made to the database
Disadvantages
- Very difficult to maintain a complex model
- Model customization is very difficult. In other words, if we don't have a foreign key in the database and we need an association in the conceptual layer then we must create it manually and it is very difficult to maintain when we update the model from the database.
Comparing Approaches:
Feature |
Entity Framework Approaches |
Code First |
Model First |
Database First |
Support ORM designer tool (visual creation of data model) |
No.
No EDMX support hence there is no support of visual creation of data model. |
Yes. |
Yes. |
Generates code and database scripts |
Yes. |
Yes. |
NA
In this approach, we already have database and from the database we create model. |
Extensible through partial classes |
NA
In this approach, all are POCO classes, |
Yes. |
Yes. |
Full control over model from code |
Yes. |
No. |
No. |
Manual changes to the database are possible? |
Yes. |
Yes. |
Yes. |
Easy to modify the model? |
Yes. |
Yes. |
Yes. |
An existing database can be used? |
Yes.
To know more click here. |
NA |
Yes. |
Selecting Right Approach:
Definitely, the development approach depends upon the project situation. The following diagram may help us to select the correct approach for your project. As in this diagram, if we already have domain classes, the Code First approach is best suited for our application. The same as if we have a database, Database First is a good option. If we don't have model classes and a database and require a visual entity designer tool then Model First is best suited.