In this Video, I am going to discuss DbContext in Entity Framework Core in detail. Please read our previous Video where we discussed how to install entity framework core in visual studio step by step. DbContext class is one of the important classes in entity framework core and at the end of this Video, you will understand the significance of the DbContext class in Entity Framework Core. As part of this, we will discuss the following pointers.

- What is a DbContext class?
- How to create and use the DbContext class?
- DbContextOptions class in Entity Framework Core
- Entity Framework Core DbSet
What is a DbContext class?
The DBContext class is basically used by our application to interact with the underlying database. That means this class is used to manage the database connection as well as also used to perform CRUD operation with the underlying database.
How to create and use the DbContext class in Entity Framework Core?
In order to use the DbContext class in your application, you need to create a class derives from the DbContext class. The DbContext class present is in Microsoft.EntityFrameworkCore namespace.
So, within the models folder create a class file with the name StudentDbContext and then inherit from the class from DbContext class as shown in the below image. You can provide any name for your DbContext class, as I am going to develop an application to manage the students, so I gave the name as StudentDbContext.
DbContextOptions class in Entity Framework Core:
In order to perform any useful task by the DbContext class, we need an instance of the DbContextOptions class. The instance of the DbContextOptions carries all the required configuration information such as the connection string, database provider, etc.
To pass the DbContextOptions instance we need to use the constructor of StudentDbContext class as shown in the image below.
In our next Video, we will discuss the DbContextOptions class in detail.
Entity Framework Core DbSet:
The entity framework code DbContext class includes a property i.e. DbSet<TEntity> for each entity in your application. Let us understand this with an example. Create three entities such as Student, Branch and Address within the Models folder as shown below.
As our application contains three entities, so in our StudentDbContext class, we need to have three DbSet Properties as shown below.
We will use the above DbSet properties such as Students, Branches, and Addresses to perform CRUD Operations.
So, the complete code of the StudentDbContex class is given below.
So, in Short,
The DbContext in Entity Framework Core perform the following tasks:
- Manage database connection
- Configure model & relationship
- Querying database
- Saving data to the database
- Configure change tracking
- Caching
- Transaction management
In order to connect to a database, we need the database connection string. So, in the next Video, I am going to discuss, where to define the connection string and how to use it in Entity Framework Core to interact with the SQL Server Database. Here, in this Video, I try to explain the need and use of the DbContext class in Entity Framework. I hope you enjoy this Video.