【发布时间】:2018-02-10 17:03:32
【问题描述】:
我的 ASP.NET Core 应用程序中有常见的 DI 用法。
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped(sp => new UserContext(new DbContextOptionsBuilder().UseNpgsql(configuration["User"]).Options));
services.AddScoped(sp => new ConfigContext(new DbContextOptionsBuilder().UseNpgsql(configuration["Config"]).Options));
}
在ConfigContext 中存在方法GetUserString,它将connectionString 返回到UserContext。
我需要AddScoped UserContext 和connectionString 来自ConfigContext
申请时UserContext。
【问题讨论】:
-
连接字符串可以根据每个请求(不同的用户)而变化吗?
-
是的,可以根据配置上下文中的逻辑而有所不同
标签: c# dependency-injection asp.net-core asp.net-core-mvc