Entity Framework Core in Action, Second Edition cover
welcome to this free extract from
an online version of the Manning book.
to read more
or

5 Using EF Core in ASP.NET Core web applications

 

This chapter covers

  • Introduction to using EF Core in ASP.NET Core
  • Using dependency injection in ASP.NET Core
  • Accessing the database in ASP.NET Core MVC actions
  • Using EF Core migrations to update a database
  • Using async/await to improve scalability

In this chapter you’ll pull everything together by using ASP.NET Core to build a real web application. Using ASP.NET Core brings in issues that are outside EF Core, such as dependency injection, which I describe later. But they’re necessary if you’re going to use EF Core in this type of application.

This chapter assumes you’ve read chapters 2 to 4 and know about querying and updating the database and what business logic is. This chapter is about where to place your database access code and how to call it in a real application. It also covers the specific issues of using EF Core in an ASP.NET Core application. For that reason, this chapter includes quite a bit about ASP.NET Core, but it’s all focused on using EF Core well in this type of application. I end with more general information on the various ways to obtain an instance of the application’s DbContext for cases such as background tasks.

5.1   Introducing ASP.NET Core

5.2   Understanding the architecture of the Book App

5.3   Understanding dependency injection

5.3.1   Why you need to learn about DI in ASP.NET Core

5.3.2   A basic example of dependency injection in ASP.NET Core

5.3.3   The lifetime of a service created by DI

5.4   Making the application’s DbContext available via DI

5.4.1   Providing information on the database’s location

5.4.2   Registering your application’s DbContext with the DI provider

5.5   Calling your database access code from ASP.NET Core

5.5.1   A summary of how ASP.NET Core works and the terms it uses

5.5.2   Where does the EF Core code live in the Book App?

5.6   Implementing the book list query page

5.7   Implementing your database methods as a DI service

5.7.1   Registering your class as a DI service

5.7.2   Injecting ChangePubDateService into the ASP.NET action method

5.7.3   Improving registering your database access classes as services

5.8   Deploying an ASP.NET Core application with a database

5.8.1   Knowing where the database is on the web server

5.8.2   Creating and migrating the database

5.9   Using EF Core’s Migrate to change the database’s structure

5.9.1   Updating your production database

5.9.2   Having your application migrate your database on startup

5.10   Using async/await for better scalability

5.10.1   Why async/await is useful in a web application using EF Core

5.10.2   Where should you use async/await with database accesses?

sitemap