- 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.
推薦閱讀
- Flask Web開發(fā)入門、進階與實戰(zhàn)
- 深入理解Java7:核心技術與最佳實踐
- INSTANT Sinatra Starter
- Python+Tableau數(shù)據(jù)可視化之美
- Building Serverless Architectures
- Scala編程(第5版)
- R語言:邁向大數(shù)據(jù)之路(加強版)
- 實戰(zhàn)Java高并發(fā)程序設計(第2版)
- 零基礎學HTML+CSS
- C++從入門到精通(第6版)
- WildFly Cookbook
- 實戰(zhàn)Python網(wǎng)絡爬蟲
- C++服務器開發(fā)精髓
- Less Web Development Cookbook
- C Primer Plus(第6版)中文版【最新修訂版】