Edit Content
Click on the Edit Content button to edit/add the content.
Categories
IT Education

Understanding the Unit of Work Pattern in C#

LINQ queries against a DbSet will be translated into queries against the database. EF Core API will create the Student and Grade table in the underlying SQL Server database where each property of these classes will be a column in the corresponding table. We created the Student and Grade domain classes in the Create Entities chapter. We can turn domain classes into entities by specifying them as DbSet type properties. This will allow Entity Framework to track their changes and perform CRUD operations accordingly.

what is dbcontext in entity framework

Reads classes and code-based configurations to build an in-memory model, metadata, and relevant database. It also manages associations between using data annotations/Fluent API. It stores the entities which have been https://deveducation.com/ retrieved during the lifetime of a context class. DbContext exposes the property (DbSet) which represent collections of entities in the context. DbContext manages these entities during the lifetime of the entities.

About DbSet and DbContext

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 the DbSet object. The Remove method works for both existing and newly added entities.

what is dbcontext in entity framework

The ModelBuilder provides the API, which is used to configure the shape, data type, relationships between the models etc. To connect to a database, we need the database connection string. So, in the next article, I will discuss advantages of entity framework where to define the Connection String in Entity Framework Core and how to use it to interact with the SQL Server Database. In this article, I explain the need and use of the DbContext Class in Entity Framework Core.

What is the difference between a “Model” and a “Context” in Entity Framework jargon?

The DbContextOptions instance carries configuration information such as the database providers to use, connection strings and any database related configuration information. It is the connection between our entity classes and the database. The DBContext is responsible for the database interactions like querying the database and loading the data into memory as entity. It also tracks the changes made to the entity and persists the changes to the database .

  • Now, we need to add one entity i.e. one Student into the Student table.
  • At the root directory of your project, create a folder and name it as Entities.
  • It stores the entities which have been retrieved during the lifetime of a context class.
  • This state is then used while saving the entity into the database, by generating the proper insert, alter, delete queries.
  • The DbContext class also allows you to override the OnModelCreating() and ConfigureConventions() methods to configure the database model and default conventions of EF Core.
  • The main class that coordinates Entity Framework Core functionality for a given data model is the database context class, which allows to query and save data.

Now that we discussed the theory, let’s dive into an example implementation. We can track changes when we use for all type of operations or execution. Now, let us proceed further and try to understand the important methods provided by the DbContext class in Entity Framework.

Then we call the BeginTransactionAsync() method, followed by the operation, namely creating a new order. This way, no database operation occurs until CommitAsync() is called. By using the Scoped lifetime, inside the same scope (or request for web applications) the UnitOfWork instance will remain the same.

what is dbcontext in entity framework

The OnConfiguring() method allows us to select and configure the data source to be used with a context using DbContextOptionsBuilder. In our upcoming article, we will discuss more about this method. Now, we are done with the initial domain classes for our application.