concept asp.net core application in category entity framework

This is an excerpt from Manning's book Entity Framework Core in Action.
Listing 2.2 provides the options for the database at the time you create the application DbContext, called
EfCoreContext. To be honest, this listing is based on what I use in my unit testing, because it has the benefit of showing you the component parts. Chapter 5, which is about using EF Core in an ASP.NET Core application, presents a more powerful way to create the application’s DbContext, by using a feature called dependency injection.
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 running parallel tasks.
The following listing shows how the application’s DbContext is registered as a service in an ASP.NET Core application. This is done in the
ConfigureServicesmethod in theStartupclass of your ASP.NET Core application, along with all the DI services you need to register.Listing 5.2 The
ConfigureServicesmethod in theStartupclass of ASP.NET Corepublic void ConfigureServices(IServiceCollection services) #1 { // Add framework services. services.AddMvc(); #2 var connection = Configuration #3 .GetConnectionString("DefaultConnection"); #3 services.AddDbContext<EfCoreContext>( #4 options => options.UseSqlServer(connection, #4 b => b.MigrationsAssembly("DataLayer"))); #5 //… other service defintions removed } #1 The method in ASP.NET to set up services #2 Sets up a series of services to use controllers, etc. #3 You get the connection string from the appsettings.json file, which can be changed when you deploy. #4 Configures the application’s DbContext to use SQL Server and provide the connection #5 You’re using EF Core’s Add-Migrations command, so you need to indicate which project your application’s DbContext is in.Your first step is to get the connection string from the application’s
Configurationclass. In ASP.NET Core, theConfigurationclass is set up during theStartupclass constructor, which reads theappsettingfiles. Getting the connection string that way allows you to change the database connection string when you deploy the code to a host. Section 5.8.1, which is about deploying an ASP.NET Core application that uses a database, covers how this works.