这是appsetting.json文件
{
"Connection Strings": {
"EmployeeDBConnection": "server=(localhost)\\SQLEXPRESS ; database=EmployeeDB; Trusted_Connection =true"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"MyKey": "Value of MYKey from appsettings.json"
}
这是startup.cs文件
namespace EmployeeManagement
{
public class Startup
{
private IConfiguration _config;
public Startup(IConfiguration config)
{
_config = config;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<IEmployeeRepository, SQLEmployeeRepository>();
services.AddControllersWithViews();
services.AddDbContextPool<AppDbContext>(options => options.UseSqlServer(_config.GetConnectionString("EmployeeDBConnection")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World");
});
}
}
}
我检查了“连接字符串”最后是否有s, 更改了连接字符串在appsetting.json文件中的位置 我将整个ConnectionString放在这里,而不是只在Startup.cs的Configuration.GetConnectionString()中写入连接字符串的名称。 我仍然遇到此错误。
答案 0 :(得分:0)
您的密钥"Connection Strings"
包含空格。更改为"ConnectionStrings"
以实现预期的行为
答案 1 :(得分:0)
"Connection Strings"
与您的参数"connectionStrings"
不匹配。您需要在appsettings.cs中进行更改。也可能需要用"connectionString"
代替,您的措辞让我有些困惑。无论如何,您的程序都在寻找"connectionString"
,而不是"Connection Strings"
。