我是 .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/