简体   繁体   English

ASP.NET API:尚未为此DbContext配置任何数据库提供程序

[英]ASP.NET API: No database provider has been configured for this DbContext

I am trying to connect to a MySql database from my .Net Core API project. 我正在尝试从我的.Net Core API项目连接到MySql数据库。

This is my context class: 这是我的上下文类:

public class MyContext : DbContext
{
    public MyContext() { }

    public MyContext(DbContextOptions<MyContext> options)
         : base(options) { }

    public MyContext(DbContextOptions options)
         : base(options) { }

        ...
}

and this is my startup.cs : 这是我的startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    string MyConnectionString = "server=localhost;port=3306;user=root;password=*****;database=my_db";
    services.AddDbContext<MyContext>(options => options.UseMySQL(MyConnectionString));
}

I know this question has already been asked a million times, but I still can't fix it. 我知道这个问题已被问过一百万次了,但我还是无法修复它。

Edit: In controllers, I instantiate my MyContext using the parameterless constructor. 编辑:在控制器中,我使用无参数构造函数实例化我的MyContext

Let's try something else : 我们试试别的:

First, in your DbContext create OnConfiguring method : 首先,在DbContext中创建OnConfiguring方法:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder
           .UseMYSQL("... connection string ...");           
    }

Secondly, on the beginning of you ConfigureServices method, configure the DbContext this way : 其次,在您的ConfigureServices方法的开头,以这种方式配置DbContext:

services.AddDbContext<MyContext>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用实体框架配置ASP.NET Core:“尚未为此DbContext配置数据库提供程序” - Configure ASP.NET Core with Entity Framework: “No database provider has been configured for this DbContext” ASP.NET核心InvalidOperationException:&#39;尚未为此DbContext配置数据库提供程序。 - ASP.NET core InvalidOperationException: 'No database provider has been configured for this DbContext./ 在 ASP.NET Core 2.1 中没有为此 DbContext 配置数据库提供程序 - No database provider has been configured for this DbContext in ASP.NET Core 2.1 尚未为此 DbContext 配置数据库提供程序 - No database provider has been configured for this DbContext InvalidOperationException:没有为此DbContext配置数据库提供程序 - InvalidOperationException: No database provider has been configured for this DbContext Azure KeyVault for DBContext:“尚未为此DbContext配置数据库提供程序” - Azure KeyVault for DBContext: “No database provider has been configured for this DbContext” C#:从 Net Core 中的 DbContext 继承,构造函数:没有为此 DbContext 配置数据库提供程序 - C#: Inherit from DbContext in Net Core, Constructor: No database provider has been configured for this DbContext InvalidOperationException:没有为此DbContext配置数据库提供程序。 可以通过覆盖DbContext来配置提供程序 - InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext 没有为此 DbContext 配置数据库提供程序。 可以通过覆盖 DbContext.OnConfiguring 来配置提供程序 - No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring 带有 GenericRepository 错误的依赖注入 - 没有为此 DbContext NET.Core 3.1 配置数据库提供程序 - Dependency Injection with GenericRepository error - No database provider has been configured for this DbContext NET.Core 3.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM