“ ConnectionString属性尚未初始化。”与json文件

时间:2019-11-19 13:01:49

标签: c# json asp.net-core connection-string

我正在尝试使用以下方法将连接字符串从json文件获取到DAL类库:

private static IConfigurationRoot config;
private static string connectionString;

public UserDAL()
{
    config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
    connectionString = config.GetConnectionString("DefaultString");
}

在json文件中:

{
  "ConnectionStrings": {
    "DefaultString": "theconnectionstring"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

当我尝试打开连接时发生错误,因此con.Open();

0 个答案:

没有答案