- 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.
推薦閱讀
- Web應用系統開發(fā)實踐(C#)
- The Android Game Developer's Handbook
- PyQt從入門到精通
- OpenNI Cookbook
- aelf區(qū)塊鏈應用架構指南
- 名師講壇:Spring實戰(zhàn)開發(fā)(Redis+SpringDataJPA+SpringMVC+SpringSecurity)
- Mastering Apache Maven 3
- WordPress 4.0 Site Blueprints(Second Edition)
- Machine Learning in Java
- Java程序設計案例教程
- 輕松上手2D游戲開發(fā):Unity入門
- 零基礎學C語言(第4版)
- Photoshop智能手機APP界面設計
- HTML5與CSS3權威指南
- Java Web動態(tài)網站開發(fā)(第2版·微課版)