- Mastering Entity Framework Core 2.0
- Prabhakaran Anbazhagan
- 137字
- 2021-07-02 21:16:41
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.
推薦閱讀
- Visual FoxPro程序設(shè)計教程(第3版)
- Flink SQL與DataStream入門、進階與實戰(zhàn)
- Python Geospatial Development(Second Edition)
- SEO智慧
- Hands-On C++ Game Animation Programming
- 零基礎(chǔ)學(xué)Python數(shù)據(jù)分析(升級版)
- OpenCV with Python By Example
- Java Web應(yīng)用開發(fā)給力起飛
- jQuery技術(shù)內(nèi)幕:深入解析jQuery架構(gòu)設(shè)計與實現(xiàn)原理
- 石墨烯改性塑料
- Julia數(shù)據(jù)科學(xué)應(yīng)用
- 3ds Max 2018從入門到精通
- SAS編程演義
- Building Web and Mobile ArcGIS Server Applications with JavaScript(Second Edition)
- Tkinter GUI Programming by Example