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

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.

主站蜘蛛池模板: 枞阳县| 乐东| 岚皋县| 武平县| 翁源县| 东莞市| 瓮安县| 兴义市| 林周县| 恩施市| 惠东县| 江永县| 梅州市| 通化市| 综艺| 镇康县| 垦利县| 育儿| 集贤县| 景德镇市| 迁安市| 龙南县| 南木林县| 武穴市| 定边县| 共和县| 石家庄市| 绥江县| 泸西县| 江北区| 平远县| 峡江县| 江陵县| 金堂县| 双城市| 南丰县| 兴隆县| 页游| 拉孜县| 秭归县| 临海市|