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:
- To change the code to conform to best practice, open TextScoreDBContext.cs, and replace the following lines of code:
protected override void OnConfiguring(DbContextOptionsBuilder ...