config.json - 在 ASP.NET vNext 中添加数据库连接字符串

config.json - Adding database connection strings in ASP.NET vNext

我正在学习ASP.NET vNext。我需要在 config.json 文件中存储两个连接字符串。我如何存储这些?我可以这样做吗:

config.json

{
  "connectionStrings" : {
    "connection-1" : "server=...",
    "connection-2" : "server=..."
  }
}

我无法找到 config.json 的架构。出于这个原因,我不确定该怎么做。我看到了 IConfiguration here 的用法。不过,我不确定 Configuration 在应用程序中是如何可用的。

This is the official Configuration repo and this 是一个很棒的博客 post 解释配置系统。

长话短说:

  1. You need one or more Microsoft.Framework.ConfigurationModel.* NuGet packages in your application. The list of available packages is here.
  2. Add the configuration sources that you need
  3. Build the config file(s)
  4. Read the configuration sources

以下是您的操作方法:

 {   
      "Data": {
        "connection-1": {
          "ConnectionString": "Server=..."
        },
        "connection-2": {
          "ConnectionString": "Server=..."
        }   
}

}

这几天是这样的...

{
  "ConnectionStrings": {
    "myDb1": "Server=myServer;Database=myDb1;Trusted_Connection=True;",
    "myDb2": "Server=myServer;Database=myDb2;Trusted_Connection=True;"
  }
}

发件人:Store and read connection string in appsettings.json