Dependency injection

The code in the previous section does not conform to the ASP.NET Core best practices. The best practice is to register the new service ( DbContext) during application startup, and inject it into the classes that are using it, such as MVC controllers, using Dependency Injection rather than initializing the context inline in the TextScoresDBContext class. To learn more about Dependency Injection, view the documentation at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection.

Let's take a look at the following steps:

  1. To change the code to conform to best practice, open TextScoreDBContext.cs, and replace the following lines of code:
 protected override void OnConfiguring(DbContextOptionsBuilder ...

Get Serverless computing in Azure with .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.