[英]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.