【问题标题】:Connection string for PostgreSQLPostgreSQL 的连接字符串
【发布时间】:2019-01-25 11:56:00
【问题描述】:

我正在尝试在我的 appsettings.json 文件中写入我的连接字符串,并通过扩展类将它带入我的启动文件,但我不断收到一个

值不能为空。参数名称:连接字符串。

我一直在使用各种示例,但似乎看不到这个带有 ASP.NET Core 2.2 启动类的新设置。`而且我正在使用 PostgreSql

appsetting.json 文件

 {
  "PostgreConnectionString": {
    "DefaultConnection": "User ID=1;Password=1234;Host=localhost;Port=5432;Database=Demo;Pooling=true;Integrated Security=true;",

"Logging": {
  "LogLevel": {
    "Default": "Warning"
  }
},
"AllowedHosts": "*"
}
}

ServiceExtension.cs

public static void ConfigurePostgreSQL(this IServiceCollection services, IConfiguration config)
        {
            var connectionString = config["PostgreConnectionString:DefaultConnection"];
            services.AddDbContext<RepositoryContext>(options => options.UseNpgsql(config.GetConnectionString(connectionString))); --Error in this line value cannot be null
        }

Startup.cs

services.ConfigurePostgreSQL(Configuration);

【问题讨论】:

  • 调试ConfigurePostgreSQL函数时能得到连接字符串吗?在我的测试中它不为空。

标签: postgresql asp.net-core connection-string


【解决方案1】:

对于 .NET Core,我使用:

_connectionString = config.GetConnectionString("PostgreConnectionString");

配置

{     
  "ConnectionStrings": {
    "PostgreConnectionString": "string"
  }
}

有效

【讨论】:

    【解决方案2】:

    问题是嵌套括号! Appsettings.json 生成一个包含在“Logging”对象中的 ConnectionStrings 对象。 把 ConnectionStrings 拿出来,你的 Configuration.GetConnectionStrings 就可以工作了!

    【讨论】:

      猜你喜欢
      • 2013-11-09
      • 2010-10-02
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      相关资源
      最近更新 更多