【发布时间】:2020-09-12 16:43:35
【问题描述】:
有没有办法依赖注入接口 DbContext 派生自 Startup 类?
下面是DbContent 类
public partial class SRSDbContext : IdentityDbContext<SRSIdentityUser>, ISRSDbContext
{
public SRSDbContext()
{
//...
在Startup 类中我尝试使用代码
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ISRSDbContext>(opts =>
opts.UseSqlServer(Configuration.GetConnectionString("sqlConnection")));
但是ISRSDbContext接口在上面这行代码中没有被识别出来。
我喜欢使用AppsSetting.json 文件中定义的连接字符串注册ISRSDbContext 以进行依赖注入
【问题讨论】:
标签: c# dependency-injection entity-framework-core asp.net-core-webapi asp.net-core-3.1