值不能为 null(参数“connectionString”).NET 6

DeV*_*eV1 1 c# asp.net-core-6.0 .net-6.0

我使用 .Net 6,当我想运行第一次迁移时,出现以下错误:

值不能为空。(参数“连接字符串”)

我的appsettings.json

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

  "AllowedHosts": "*"
}
Run Code Online (Sandbox Code Playgroud)

这是我的Configuration方法program.cs

builder.Services.AddDbContext<SolarDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("ConnectionStrings")));
Run Code Online (Sandbox Code Playgroud)

Ser*_*rge 7

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

builder.Services.AddDbContext<SolarDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
Run Code Online (Sandbox Code Playgroud)