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

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.

主站蜘蛛池模板: 汨罗市| 绥化市| 黄梅县| 南康市| 泾川县| 上思县| 台东县| 湘潭县| 申扎县| 留坝县| 桃源县| 巴中市| 息烽县| 定边县| 荃湾区| 定西市| 丰原市| 西畴县| 宜兰县| 新乡县| 阜新市| 富锦市| 建平县| 滦平县| 天镇县| 桃园市| 宜章县| 山丹县| 南康市| 滦南县| 巴青县| 黄平县| 潮州市| 朝阳区| 神农架林区| 丹棱县| 沂南县| 青河县| 宜黄县| 武隆县| 碌曲县|