
上QQ阅读APP看书,第一时间看更新
Refactoring the ConfigureServices method
We will use the ConfigureServices() method to include the database context framework service to the service collection. Add the following using statements to configure the DbContext options and to add database context to the services list:
using Microsoft.EntityFrameworkCore;
using MasteringEFCore.DatabaseFirst.Final.Models;
As we did in Chapter 1, Kickstart - Introduction to Entity Framework Core, we will configure context as a service and add DbContext (created using UseSqlServer() through DbContextOptionsBuilder) to the services collection:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<MasteringEFCoreDbFirstContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString(
"DefaultConnection")));
services.AddMvc();
}
We have configured the database context in the framework, but if we watch closely, we see that the connection string is coming from a configuration. Next, we will see how the configuration is included in appsettings.json.