In this session of the ASP.NET Core tutorial, we introduce Entity Framework Core (EF Core) and make our Web App ready to work with EF Core by installing required NuGet Packages. EF Core is a version of Microsoft Entity Framework that is lightweight, Open Source, Cross-functional and extensible. This data access framework is an Object Relational Mapper (ORM) that helps developers to work with DB through .NET objects.
What is Entity Framework Core (EF Core)?
Entity Framework Core is a version of Microsoft Data Access Technology, that is lightweight, Cross-functional, Open-Source, and Extensible. EF Core helps developers to work with DB with .NET objects. This framework is an object-relational mapper (ORM).
This framework is between Domain Classes and DB Context Class in our Web App and Database to handle DB-related tasks which help developers to write less code for this purpose.
Entity Framework Core Approaches (Code First – Database First)
There are two approaches for EF Core to make the relation between the Web App and the DB which are Code First and DB First. As their names imply, in Code First approach, developers should create Domain classes and DB Context Class. Then EF Core based on the Models and Context class create the Database and its related tables. Just having said that, for creating DB Context Class the Web App needs some NuGet Packages that we talk about them later.
As mentioned above, the second approach is DB First and as expected in this approach, Domain Classes and DB Context Class is created based on the existing database by EF Core.
Entity Framework Core Provider
Providers enable EF Core to connect Web App with different Database Engines same as SQL Server, MySQL, Oracle, etc. Thus, we need to install the respective NuGet Package based on the selected database engine. Therefore, you can find the list of the Providers HERE which you need to install based on your selected database.
NuGet Packages to Work with EF Core
To start working with EF Core, we need to install some NuGet Packages. For this purpose, we can use Manage NuGet Packages tools which is a graphical tool. We lunch this tool by right-clicking on Dependencies at the root of the project and selecting Manage NuGet Packages option. Then, we can search the required packages in the Browse tab and install them.
List of the NuGet packages that we need to install to work with Entity Framework Core:
- Microsoft.EntityFrameworkCore : This is the fundamental package and we need it for creating the DB Context Class.
- Microsoft.EntityFrameworkCore.Relational : For working with the relational database same as SQL Server, MySQL, etc. we need to install this packages in addition to the respective provider package.
- Microsoft.EntityFrameworkCore.SqlServe : This package is a provider which help us to work with Microsoft SQL Server Databse engine.
- Microsoft.EntityFrameworkCore.Tools : This package helps us to use NuGet Package Manager Console in Visual Studio.
DB Context Class
We create this class insider Models folder and it should be inherited from DBContext Class. DBContext class is in Microsoft.EntityFrameworkCore namespace. Then, we need to add the Constructor for the class that we can do by pressing Ctrl+. keys. Next, we should modify the constructor code and add this class to DBContextOption.
public class WebAppDBContext : DbContext
public WebAppDBContext(DbContextOptions<WebAppDBContext> options) : base(options)
In the upcoming sessions, we will develop this class more and configure DbContextOptions in Startup Class.