这就是我目前在Startup.cs中的ConfigureServices方法中添加DbContext的方法:
public void ConfigureServices(IServiceCollection services)
{
.....
services.AddDbContext<MyDbContext>(options =>
options.UseMySQL(Configuration.GetConnectionString("DefaultConnection")));
.....
}
Run Code Online (Sandbox Code Playgroud)
我的连接字符串存储在我的appsettings.json文件中,例如:
{
....
"ConnectionStrings": {
"DefaultConnection": "server=localhost;user id=root;password=root;database=mydb;sslmode=none"
}
....
}
Run Code Online (Sandbox Code Playgroud)
如果我想切换正在连接的数据库,如果它是"开发"与"生产"环境,如何使"services.AddDbContext()"切换数据库?