首页 > 解决方案 > 部分服务无法构建dbcontext

问题描述

我正在从 asp.net 项目迁移一个 asp.net core 3.1。在这里,我有一个 dbcontext,我需要从我的 appservice 类之一传递连接字符串。要使用 appservice,在这个 dbcontext 中我使用了如下内容:

public class MyContext: DbContext
{
  private readonly IAppSetting _appSetting;

  public MyContext(IAppSetting appSetting) 
  {
      _appSetting = appSetting;
  }
  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  {
              optionsBuilder.UseSqlServer(_appSetting.Myconnectionstring, opts=> opts.CommandTimeout((int)TimeSpan.FromMinutes(1).TotalSeconds));
  }
}

在 StartUp.cs 文件中,我配置了这个上下文服务,如下所示:

public void ConfigureServices(IServiceCollection services)
{
  services.AddDbContext<MyContext>();
}

但是当我运行应用程序时,它会抛出运行时错误,例如:

某些服务无法构造 dbcontext。

请帮忙。

标签: entity-framework-6asp.net-core-3.1

解决方案


推荐阅读