C# 而我';正在使用asp.net core 3.1的m获取此错误值不能为null。(参数&x27;连接字符串&x27;)
System.ArgumentNullException:'值不能为null。(参数“connectionString”) 我收到了这个错误,我认为这个问题来自appsetting.json,但我找不到它C# 而我';正在使用asp.net core 3.1的m获取此错误值不能为null。(参数&x27;连接字符串&x27;),c#,asp.net-core,model-view-controller,C#,Asp.net Core,Model View Controller,System.ArgumentNullException:'值不能为null。(参数“connectionString”) 我收到了这个错误,我认为这个问题来自appsetting.json,但我找不到它 "AllowedHosts": "*", "ConnectionString": { "EmployeeDbConnection" : "server=(localdb)\\MS
"AllowedHosts": "*",
"ConnectionString": {
"EmployeeDbConnection" : "server=(localdb)\\MSSQLLocalDB;database=EmployeeDB;Trusted_Connection=true;"
}
从_config.GetConnectionString(“EmployeeDbConnection”)引发错误
公共启动(IConfiguration配置)
{
_config=config;
}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
//有关如何配置应用程序的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=398940
public void配置服务(IServiceCollection服务)
{
services.AddDbContextPool(options=>options.UseSqlServer(_config.GetConnectionString(“EmployeeDbConnection”));
services.addScope();
services.AddMvc(options=>options.EnableEndpointRouting=false);
}
原因在于您的appsettings.json
:
将代码更改为(ConnectionString
更改为ConnectionString
):
是的,这解决了我的问题。我还错过了appsettings.json中ConnectionString中的s。谢谢
public Startup(IConfiguration config)
{
_config = config;
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<AppDbContext>(options => options.UseSqlServer(_config.GetConnectionString("EmployeeDbConnection")));
services.AddScoped<IEmployeeRepository, SqlServerRepository>();
services.AddMvc(options => options.EnableEndpointRouting = false).AddXmlSerializerFormatters();
}
"AllowedHosts": "*",
"ConnectionStrings": {
"EmployeeDbConnection" : "server=(localdb)\\MSSQLLocalDB;database=EmployeeDB;Trusted_Connection=true;"
}