簡體   English   中英

當我在 asp.net core 3.1 中工作時收到此錯誤值不能為空。 (參數'connectionString')

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

System.ArgumentNullException: '值不能為空。 (參數'connectionString')

我收到此錯誤我認為該問題來自 appsetting.json 但我找不到它

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

錯誤來自 _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();
        }

原因在您的appsettings.json

將代碼更改為( ConnectionStringConnectionStrings ):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM