当我在 asp.net core 3.1 中工作时收到此错误值不能为空。(参数'connectionString')

Rem*_*yev 3 c# model-view-controller asp.net-core

System.ArgumentNullException: '值不能为空。(参数'connectionString')

我收到此错误我认为该问题来自 appsetting.json 但我找不到它

 "AllowedHosts": "*",
    "ConnectionString": {
        "EmployeeDbConnection" : "server=(localdb)\\MSSQLLocalDB;database=EmployeeDB;Trusted_Connection=true;"
    }
Run Code Online (Sandbox Code Playgroud)

错误来自 _config.GetConnectionString("EmployeeDbConnection)

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();
        }
Run Code Online (Sandbox Code Playgroud)

Yin*_*qiu 5

原因在你的appsettings.json

将代码更改为( ConnectionStringto ConnectionStrings):

 "AllowedHosts": "*",
   "ConnectionStrings": {
    "EmployeeDbConnection" : "server=(localdb)\\MSSQLLocalDB;database=EmployeeDB;Trusted_Connection=true;"
}
Run Code Online (Sandbox Code Playgroud)