相关疑难解决方法(0)

如何在ASP.net Core中基于环境添加DbContext

这就是我目前在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()"切换数据库?

c# entity-framework-core asp.net-core

3
推荐指数
1
解决办法
1626
查看次数

标签 统计

asp.net-core ×1

c# ×1

entity-framework-core ×1