In this Video, I am going to discuss DbContext class in Entity Framework Database First Approach. Please read our previous Video where we discussed Model Browser in Entity Framework. At the end of this Video, you will understand the need and use of the Entity Framework DbContext class in detail.
What is DbContext in Entity Framework Database First Approach?. The Complete Microsoft Entity Framework Developer Course 2023 [Videos].
Note: We are going to work with the same example that we created in our Introduction to Entity Framework Database First Approach Video. Please read our introduction to Entity Framework Database First Video before proceeding to this Video.
Entity Framework DbContext Class:
Whenever we use Entity Framework Database First Approach to interact with an existing database, then the EDM (Entity Data Model) creates a class called DbContext for us. Every Entity Data Model (EDM) generates one context class for the database. In order to verify this, expand the .edmx file in the solution explorer and open <EDM Name>.Context.tt as shown in the below image. In our example, it will be StudentDataModel.Context.tt.
This T4 template file generates a context class whenever you change the Entity Data Model (.edmx file). You can see the context class file by expanding StudentDataModel.Context.tt. The context class resides in the <EDM Name>.context.cs file. In our case the context class file name is StudentDataModel.Context.cs. The default context class name is <DB Name>Entities. For example, the context class name for EF_Demo_DB is EF_Demo_DBEntities and derived from the DBContext class as shown below.
Note: The class that is derived from the System.Data.Entity.DbContext class is called a context class in the entity framework.
What are the responsibilities of DbContext class in Entity Framework?
The DbContext class in Entity Framework is an important class and is primarily responsible for interacting with data as objects. It acts as a bridge between your domain classes or entity classes and the underlying relational database. For better understanding, please have a look at the following image.
Let us understand what are the responsibilities done by DbContext class in the Entity Framework. The DbContext class is responsible for the following activities in Entity Framework:
- Querying: Mostly when we are working with Entity Framework, we used Linq Queries. The DbContext class is responsible for converting the LINQ-to-Entities queries to SQL query which is understood by the underlying database and then sends them to the database.
- Change Tracking: In our Entity States in Entity Framework Video we discussed the different states of an Entity and it is also possible to change the state from one state to another. The Entity Framework DbContext class is also responsible for keeps track of changes that occurred on the entities after querying from the database.
- Persisting Data: The DbContext class is also responsible for performs the Insert, Update and Delete operations to the database which is persistent data permanently to the database.
- Caching: Nowadays caching plays an important role in almost all types of applications to improve performance. By default, the DbContext class provides the first-level cache. That means it stores the entities which have been retrieved from the database during the lifetime of a context class.
- Manage Relationship: It Manages the relationships among the entities using CSDL, MSL, and SSDL in Db-First or Model-First approach, and using fluent API configurations in the Code-First approach.
- Object Materialization: The DbContext is responsible for converting the raw data retrieved from the database into entity objects in Entity Framework.
Note: If you are not understanding the above points then dont worry, we will discuss the above points in detail with examples in our upcoming Video.
The following is an example of EF_Demo_DBEntities class (context class that derives from DbContext) generated with EDM for the EF_Demo_DB database in the previous Video.
As you can see in the above code, the context class (EF_Demo_DBEntities) includes the entity set of type DbSet<TEntity> for all the tables and views. In our next Video, we will discuss the DbSet class in detail. It also includes functions for the stored procedures and functions. In our example, we dont have any functions but we have stored procedures and for each stored procedure its creates separate functions. The OnModelCreating method allows us to configure the model using the DbModelBuilder Fluent API in Entity Framework.
- Entry: Gets a DbEntityEntry for the given entity. The entry provides access to change tracking information and operations for the entity.
- SaveChanges: Executes INSERT, UPDATE, and DELETE commands to the database for the entities with Added, Modified, and Deleted states.
- SaveChangesAsync: Asynchronous method of SaveChanges()
- Set: Creates a DbSet<TEntity> that can be used to query and save instances of TEntity.
- OnModelCreating: Override this method to further configure the model that was discovered by convention from the entity types exposed in DbSet<TEntity> properties on your derived context.
- ChangeTracker: Provides access to information and operations for entity instances that this context is tracking.
- Configuration: Provides access to configuration options.
- Database: Provides access to database-related information and operations.
Using DbContext class we can perform the CRUD operation. Let us perform the four operations using DbContext class.
Adding New Entity in Entity Framework Database First Approach:
Now, we need to add one entity i.e. one Student into the Student table. Adding a new object with the Entity Framework is very simple. Just create an instance of the entity and then register it using the Add method on DbSet and call the SaveChanges method. The following piece of code in the main method does the same. So, please modify the main method of the Program class as shown below.
Output: You can see the newly added student in the console window and you can also verify the same in the Student table of the EF_Demo_DB database.
Updating Existing Entities in Entity Framework Database First Approach
Now, we need to update one entity i.e. we need to update the First name and Last name of the newly created student i.e. the student whose student id is 5. The following piece of code in the main method exactly does the same. So, please modify the main method of the Program class as shown below.
Output: You can see the updated data in the console window and you can also verify the same in the Student table of the EF_Demo_DB database.
Deleting an Entity in Entity Framework Database First Approach
Now, we need to delete the newly created entity i.e. the student whose student id is 5. To delete an entity using Entity Framework, we need to use the Remove method on DbSet. The Remove method works for both existing and newly added entities. Calling Remove on an entity that has been added but not yet saved to the database will cancel the addition of the entity. The entity is removed from the change tracker and is no longer tracked by the DbContext. Calling Remove on an existing entity that is being change-tracked will register the entity for deletion the next time SaveChanges is called. The following example shows an instance where the student is removed from the database whose id is 5.
Output: Now, you can observe the deleted data not showing in the Console. You can also verify the same in the Student table of the EF_Demo_DB database.
Points to Remember:
- DbContext API is mostly targeted at simplifying your interaction with Entity Framework.
- It also reduces the number of methods and properties you need to access commonly used tasks.
- The context class manages the entity objects during run time, which includes populating objects with data from a database, change tracking, and persisting data to the database.