Configuring the DbContext class
To represent the database, EF Core uses the DbContext
class, which allows us to query and save data. An instance of the DbContext
class maintains the database connection and maps the database schema to the object model. It also tracks the changes in objects and manages the transactions. If you are familiar with OOP, you can think of the DbContext
class as a bridge between the database and the object model, just like an interface. When you query or save data, you operate the objects through the DbContext
class, and EF Core will translate the operations to the corresponding SQL statements.
In this chapter, we will develop a simple application to manage invoices. This application will be used to demonstrate how to use EF Core to access the database, including how to define the database schema, how to perform CRUD operations, and how to use migrations to update the database schema.
You can follow Chapter 1 to define the API contract first. The API...