官术网_书友最值得收藏!

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 1Kickstart - 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.

主站蜘蛛池模板: 遂川县| 屯门区| 阜平县| 介休市| 安达市| 班玛县| 乳山市| 正定县| 宜昌市| 中方县| 玉门市| 华坪县| 浮山县| 芦溪县| 扶风县| 芮城县| 商城县| 金昌市| 江山市| 广德县| 阳朔县| 陇西县| 沙田区| 荃湾区| 绥滨县| 永顺县| 信宜市| 浑源县| 和龙市| 章丘市| 沁阳市| 长沙县| 岳阳市| 时尚| 酒泉市| 烟台市| 故城县| 阿克苏市| 斗六市| 岳池县| 绍兴市|