Loading

ASP.NET Core

What is DbContext in Entity Framework Core?. The Complete ASP.NET Core Developer Course 2023 [Videos].

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.

  1. What is a DbContext class?
  2. How to create and use the DbContext class?
  3. DbContextOptions class in Entity Framework Core
  4. 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.

How to create and use the DbContext class in Entity Framework Core

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.

DbContextOptions class in Entity Framework Core

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.

namespace FirstCoreMVCApplication.Models
{
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public int BranchId { get; set; }
}
public class Branch
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Address
{
public int Id { get; set; }
public int StudentId { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
public string Pin { get; set; }
}
}

As our application contains three entities, so in our StudentDbContext class, we need to have three DbSet Properties as shown below. 

Entity Framework Core DbSet

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.

using Microsoft.EntityFrameworkCore;
namespace FirstCoreMVCApplication.Models
{
public class StudentDbContext : DbContext
{
public StudentDbContext(DbContextOptions<StudentDbContext> options)
: base(options)
{
}
public DbSet<Student> Students { get; set; }
public DbSet<Branch> Branches { get; set; }
public DbSet<Address> Addresses { get; set; }
}
}
So, in Short,

The DbContext in Entity Framework Core perform the following tasks:

  1. Manage database connection
  2. Configure model & relationship
  3. Querying database
  4. Saving data to the database
  5. Configure change tracking
  6. Caching
  7. 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.

See All

Comments (624 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET Core / Blog

What is ASP.NET Core?

ASP.NET Core is the new version of the ASP.NET web framework mainly targeted to run on .NET Core platform.
27-jan-2022 /16 /624

ASP.NET Core / Blog

What is ASP.NET Core Framework?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. With ASP.NET Core, you can: Build web apps and services, Internet of Things (IoT) apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux.
27-jan-2022 /16 /624

ASP.NET Core / Blog

How to Setup ASP.NET Core Environment ?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. With ASP.NET Core, you can: Build web apps and services, Internet of Things (IoT) apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux.
27-jan-2022 /16 /624