Unable to run commands. Error: No database provider has been configured for this DbContext

Unable to run commands. Error: No database provider has been configured for this DbContext

我无法针对我创建的基本 .NET Core 2.2 Web API 运行 EntityFramework 核心命令。 API 正在工作,但我不能 'add-migration' 或 'update-database' 除非我更改检索连接字符串的位置。我相信第一个示例是最佳实践,因为连接字符串更安全,但在尝试 运行 EF Core 命令时出现错误。

"No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext."

据我所知,我在传递 DbContextOptions<> 时正确使用了 AddDbContext()。唯一的代码差异在于 Startup.ConfigureServices() 和 MyContext.OnConfiguring()。 我的首选示例有什么问题?

首选示例(EF 命令不起作用)

// MyContext.cs
public class MyContext : DbContext
  {
    private readonly DbContextOptions<MyContext> _options;
    private readonly IConfiguration _config;

    public MyContext (DbContextOptions<MyContext> options, IConfiguration config) : base(options)
    {
      _options = options;
      _config = config;
    }

    public DbSet<Account> Accounts { get; set; }
    public DbSet<User> User { get; set; }
  }
}

// Startup.cs
public class Startup
  {
    public IConfiguration Configuration { get; }

    public Startup(IConfiguration configuration)
    {
      Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
      services.AddDbContext<MyContext>(
        options => options.UseSqlServer(Configuration.GetConnectionString("MyAPI")));
      services.AddScoped<IMyRepository, MyRepository>();

      services.AddAutoMapper();

      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
      if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); }
      else { app.UseHsts(); }

      //app.UseHttpsRedirection();
      app.UseMvc();
    }
  }

使用下面的代码,我可以 运行 'add-migration' 和 'update-database' 没有错误,但我相信通过这种方式检索连接字符串不太安全。

示例 2(EF 命令有效)

// MyContext.cs
public class MyContext : DbContext
  {
    private readonly DbContextOptions<MyContext> _options;
    private readonly IConfiguration _config;

    public MyContext (DbContextOptions<MyContext> options, IConfiguration config) : base(options)
    {
      _options = options;
      _config = config;
    }

    public DbSet<Account> Accounts { get; set; }
    public DbSet<User> User { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
      optionsBuilder.UseSqlServer(_config.GetConnectionString("MyAPI"));
    }
  }
}

// Startup.cs
public class Startup
  {
    public IConfiguration Configuration { get; }

    public Startup(IConfiguration configuration)
    {
      Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
      services.AddDbContext<MyContext>();
      services.AddScoped<IMyRepository, MyRepository>();

      services.AddAutoMapper();

      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
      if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); }
      else { app.UseHsts(); }

      //app.UseHttpsRedirection();
      app.UseMvc();
    }
  }

您只需指定创建数据库连接的位置。

dotnet ef migrations add SomeMigration --startup-project ../path/to/Api/project

在此示例中,您是 运行 迁移命令并指定包含数据库上下文 configuration/set up 的项目的路径。如果您不指定它,并且设置不在 DbContext class 中,那么 EF 不知道应该如何配置数据库。

为我解决问题的是更新到 .NET Core 3.1.101 和 EFCore 3.1.1。