简体   繁体   中英

While I'm working ins asp.net core 3.1 getting this error Value cannot be null. (Parameter 'connectionString')

System.ArgumentNullException: 'Value cannot be null. (Parameter 'connectionString')

I am getting this error I think that problem brings from appsetting.json but I can't find it

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

Error bring from _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();
        }

The reason is in your appsettings.json :

Change the code to( ConnectionString to ConnectionStrings ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM