DbContext Class in Asp.Net

 

A Dbcontext class is a class that represents a session with a database and provides a set of APIs for performing database operations. This is part of the Entity Framework Core packages.

 

The Dbcontext class is responsible for 

  • Maintaining a connection to the database, 
  • Tracking changes to data 
  •  interacting with the database & performing database CRUD operations such as query, inserting, updating and deleting.
  • Create a bridge between your domain model classes and the database.

 

Whenever a call comes to a controller, in order to get or retrieve a data, the controller will have to talk to the dbcontext and it is the responsibility of the Dbcontext to get the data from the database and provide it back to the controller.

 

 

Create a Db Context Class

 

 

To generate constructor on class name click CTRL + .

 

 

 

DBSet Properties

Properties will be of type Dbset. Type of this set property will be the domain models (model inside domain folder)

 

 

 

 

 


Related Question