【问题标题】:Value cannot be null (Parameter 'connectionString') .NET 6值不能为空(参数“connectionString”).NET 6
【发布时间】:2021-12-29 12:17:55
【问题描述】:

我使用 .Net 6,当我想运行我的第一次迁移时,我收到此错误:

值不能为空。 (参数'connectionString')

我的appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=Solardb;integrated security=SSPI"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },

  "AllowedHosts": "*"
}

这是我在program.cs 中的Configuration 方法:

builder.Services.AddDbContext<SolarDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("ConnectionStrings")));

【问题讨论】:

  • 您尝试获取的连接字符串称为“DefaultConnection”,而不是“ConnectionsStrings”。
  • 您似乎使用 C#。该语言与 C 非常不同。

标签: c# .net-6.0 asp.net-core-6.0


【解决方案1】:

您不需要获取整个部分。 GetConnectionString 已经做到了。您只需要输入连接字符串的名称

builder.Services.AddDbContext<SolarDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

【讨论】:

    猜你喜欢
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多