无法运行命令。错误:尚未为此DbContext配置数据库提供程序 [英] Unable to run commands. Error: No database provider has been configured for this DbContext

查看:3207
本文介绍了无法运行命令。错误:尚未为此DbContext配置数据库提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am unable to run EntityFramework Core commands against the basic .NET Core 2.2 Web API I created. The API is working, but I can not 'add-migration' or 'update-database' unless I change where the connection string is retrieved. I believe the first example is best practice because the connection string is more secure, but I get an error when trying to run EF Core commands.


尚未为此DbContext配置任何数据库提供程序。可以通过重写DbContext.OnConfiguring方法或在应用程序服务提供程序上使用AddDbContext来配置提供程序。如果使用了AddDbContext,那么还请确保您的DbContext类型接受在其构造函数中创建一个DbContextOptions对象,并将其传递给DbContext的基本构造函数。

在传递DbContextOptions<>时,我已正确使用AddDbContext()。唯一的代码差异是Startup.ConfigureServices()和MyContext.OnConfiguring()。 我的首选示例在做什么?

As far as I can tell I have correctly used AddDbContext() while passing DbContextOptions<>. The only code differences are in Startup.ConfigureServices() and MyContext.OnConfiguring(). What am I doing wrong with my preferred example?

首选示例(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没有错误,但是我认为这种方式检索连接字符串的安全性较低。

With the code below I am able to run 'add-migration' and 'update-database' with no errors, but I believe the retrieval of the connection string is less secure this way.

示例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();
    }
  }


推荐答案

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

What fixed the issue for me was updating to .NET Core 3.1.101 and EFCore 3.1.1.

这篇关于无法运行命令。错误:尚未为此DbContext配置数据库提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆