c# - Entity Framework Core 添加迁移给出的值不能为空。 (参数 'connectionString')

标签 c# asp.net-core entity-framework-core ef-code-first

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

Value cannot be null. (Parameter 'connectionString')

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

应用程序设置.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": "*"
}

启动.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>();
}

最佳答案

在您的示例中,您拼写错误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": "*"
    }

关于c# - Entity Framework Core 添加迁移给出的值不能为空。 (参数 'connectionString'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62337240/

相关文章:

c# - Dictionary<string, List<int>> 复制到另一个Dictionary

asp.net-core - 没有 jQuery 的 Asp.Net Core 验证

c# - GitHub 上是否提供适用于 ASP .Net Core 3.1 的 ChangePasswordAsync?

c# - 在 Entity Framework Core 2.0 中执行 SQL 命令删除表中的所有数据

c# - sqlite 数据库中的 EntityFramework 核心格式 DateTime

Twitter API 的 C# DateTime created_at 字段

C# 等效于 Java 的 mkdirs()

c# - 如何开始用 C# 开发 Sphero-Ball Windows 应用程序?

asp.net-core - asp.net 5中IAuthenticationFilter接口(interface)在哪里

asp.net-core - 如何在 blazor 服务器端关闭页面时获取事件?