0

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

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

我的appsettings.json

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

  "AllowedHosts": "*"
}

这是我的Configuration方法program.cs

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

1 回答 1

6

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

builder.Services.AddDbContext<SolarDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
于 2021-12-29T12:18:57.460 回答