【问题标题】:Unable to run commands. Error: No database provider has been configured for this DbContext无法运行命令。错误:没有为此 DbContext 配置数据库提供程序
【发布时间】:2020-03-05 04:26:37
【问题描述】:

我无法针对我创建的基本 .NET Core 2.2 Web API 运行 EntityFramework Core 命令。 API 正在工作,但我无法“添加迁移”或“更新数据库”,除非我更改检索连接字符串的位置。我相信第一个示例是最佳实践,因为连接字符串更安全,但在尝试运行 EF Core 命令时出现错误。

"没有为此 DbContext 配置数据库提供程序。可以通过覆盖 DbContext.OnConfiguring 方法或在应用程序服务提供程序上使用 AddDbContext 来配置提供程序。如果使用 AddDbContext,则还要确保您的 DbContext type 在其构造函数中接受一个 DbContextOptions 对象并将其传递给 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();
    }
  }

【问题讨论】:

  • 这从未解决?
  • 这个项目是我很久没接触过的PoC。似乎已经解决了。我可以使用示例 1 并成功运行 EntityFramework 命令。我已经删除了我在上一条评论中提到的唯一答案的代码(仅当 optionsBuilder.IsConfigured = false 时在 OnConfiguring() 中运行 .UseSqlServer())。我在 Visual Studio 19 中运行 dotnetcore 3.1.101 和 EFCore 3.1.1。
  • 我在 2019 年 5 月之前无法访问我的版本控制,所以据我所知,唯一真正改变的是我从 dotnetcore 2.2 切换到 3。

标签: c# entity-framework-core .net-core-2.2


【解决方案1】:

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

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

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

【讨论】:

  • 我在执行 dotnet ef migrations add Testing2 --startup-project C:\path\to\Api\project\ 时遇到同样的错误。如果我发布堆栈跟踪会有帮助吗?
  • 您还应该从持久性项目所在的同一目录运行命令。因此,如果您有一个名为 Data 的项目和一个名为 API 的项目,请在 @987654324 中运行命令@ 目录并在 --startup-project 标志中提供 API 项目的相对或绝对路径
  • 我有一个项目,其中包含我的控制器和数据。我已经从项目文件夹本身和项目中的数据文件夹运行此命令,同时每次都将我的项目的绝对路径定位为 --startup-project 标志。我不断收到相同的初始错误。
  • 现在我已经添加了一个 IF 块,仅当 optionsBuilder.IsConfigured = false 时才在 OnConfiguring() 中运行 .UseSqlServer()。 if (!optionsBuilder.IsConfigured) { optionsBuilder.UseSqlServer(_config.GetConnectionString("ScanAPI")); }
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2020-05-08
    • 2021-05-04
    • 2019-03-25
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 2017-09-02
    • 2019-02-17
    • 2016-11-15
    相关资源
    最近更新 更多