Entity Framework Core 添加迁移给出的值不能为空。(参数“连接字符串”)

Cav*_*man 2 c# ef-code-first entity-framework-core asp.net-core

我是 .NET Core 的新手,正在学习 Udemy 课程。我正在尝试启用迁移以代码优先,但我不断收到错误

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

我做了一些研究,似乎大多数时候文件ConnectionStrings中的拼写错误appsetttings.json。然而,我已经检查了我的所有内容并尝试了几次复制/粘贴以确保我的拼写正确。要么是别的东西,要么是我看得太仔细而忽略了一些东西。

应用程序设置.json

{
    "ConnectionStings": {
        "DbConnection": "Server=DAVIDPC\\SQLEXPRESS01;Database=dotnet-rpg;Trusted_Connection=true;MultipleActiveResultSets=true;"
    },
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information"
        }
    },
    "AllowedHosts": "*"
}
Run Code Online (Sandbox Code Playgroud)

启动.cs

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<DataContext>(x => x.UseSqlServer(Configuration.GetConnectionString("DbConnection")));
    services.AddControllers();
    services.AddAutoMapper(typeof(Startup));
    services.AddScoped<ICharacterService, CharacterService>();
}
Run Code Online (Sandbox Code Playgroud)

小智 5

在您的示例中,您拼错了ConnectionStrings

 {
        "ConnectionStrings": {
            "DbConnection": "Server=DAVIDPC\\SQLEXPRESS01;Database=dotnet-rpg;Trusted_Connection=true;MultipleActiveResultSets=true;"
        },
        "Logging": {
            "LogLevel": {
                "Default": "Information",
                "Microsoft": "Warning",
                "Microsoft.Hosting.Lifetime": "Information"
            }
        },
        "AllowedHosts": "*"
    }
Run Code Online (Sandbox Code Playgroud)