Hello friends, I hope you all are doing great. In today's tutorial, we will have a look at what is DbContext Class in Entity Framework Core. It's our 17th tutorial in ASP.NET Core series. In our previous tutorial, we have discussed Entity Framework Core in detail and have also installed it in our web application.So, now we need understand how to work with Entity Framework Core and for that we have to look at its classes & functions. So, today we will discuss one of its class named DBContext in detail:
DbContext Class in Entity Framework Core
- EF Core contains an important class named DbContext, which is mainly responsible for communication between Database & software objects.
- We communicate with our database using this DbContext class and we can also map our fetched data to software objects i.e. properties.
- So, in order to use this DbContext class, we have to create a new class and then inherit it from DbContext Class.
- When we inherit our new class from DBContext Class, then we will be able to call all its members from our new class.
- So, I am going to create a new class in Models folder, as Models are going to communicate with the database.
- I have named this class TepDbContext and have inherited it from DbContext class, as shown in figure on right side.
DbContextOptions class in EF Core
- DbContextOptions class in EF Core contains all the configuration information about database i.e. database name, database provider, database connection string etc.
- We need to use DbContextOptions class along with DbContext class, so let's create a Constructor of DBContext class, as shown in below figure:
- You can see in above figure that I have created a constructor of TEPDbContext class and then instantiated DbContextOptions class as a parameter.
- After that, I have provided TepDbContext as a parameter inside < >, thus this option class is applied to our newly created TepDbContext class.
- Next, I have created its object titled options and finally called the base constructor from DBContext class and provided this options object as a parameter.
DbSet Property in EF Core
- So far, we have discussed two classes from EF Core and now it's time to have a look at this property in EF Core titled DBSet.
- DBSet Property is used to map the data from software objects to underlying database.
- Currently, we have just Engineers.cs file, which has the data properties, that need to be stored in our database.
- So, let's create a new property in our TepDBContext class named DBSet, as shown in below figure:
- As you can see we have created a new DbSet Property of type Engineers and given it a name DbEngineers. ( We will use it later )
So, we have successfully created our TepDbContext
class and have updated it, so, now its time to register this newly created class with dependency injection of ASP.NET Core.
Database Connection String in App Settings
- We need to provide authentication settings for our database, which we will add in appsettings.json file, so that we could use it anywhere in our project.
- I am using MySQL database and thus provided its connection string, as shown in below figure:
- You can see in above code that I have created a new section named ConnectionStrings and inside it, I have create DbConnection variable and have assigned DB connection string to it.
- In the connection string, I have first provided the server i.e. localdb and then provided name of the database i.e. TepDB and finally I have declared it a trusted connection.
- This MSSQLLocalDB is already available in Visual Studio and we will discuss it in detail in our coming lectures.
- When we upload our web application on a real server then we will change these server settings but for now, we will use localdb available.
Dependency Injection for DbContext class
- We have studied Dependency Injection in ASP.NET Core in detail, so now let's register TepDbContext class & provide SQL database connection string using Dependency Injection
- For that, open your startup.cs file and in ConfigureServices method, on IServicesCollection instance, we have called AddDbContextPool method, as shown in below figure:
- We have also specidified that we are using SQL Server & have provided the database connection string as well, I have placed a red boundary across it.
- In order to get the Connection string from appsettings.json file, I have injected IConfiguration using Constructor Injection and have placed a green boundary across it.
- So, our TepDBContext class is now registered with ASP.NET Core and we have also specified the Database provider & Connection String.
So, that was all for today. We have completed all our settings for database but we haven't yet created it. So, in our next lecture, we will see How to Create Database using EF Core Migrations. Till then take care & have fun !!!