[Fixed] The configuration file ‘appsettings.json’ was not found and is not optional

When you try to use the appsettings.json file in one project and set the startup project as another project. You will come across this error. Below is the complete error log.

Unhandled exception. System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'C:\Users\user\OneDrive - jdbots\Visual Studio\EFDemo\ConsoleApp\bin\Debug\netcoreapp3.1\appsettings.json'.
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.HandleException(ExceptionDispatchInfo info)
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load()
   at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
   at DataAccessLayer.Models.EmployeeDBContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) in C:\Users\user\OneDrive - jdbots\Visual Studio\EFDemo\DataAccessLayer\Models\EmployeeDBContext.cs:line 29
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
   at Microsoft.EntityFrameworkCore.DbContext.get_Model()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.CheckState()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Where[TSource](IQueryable`1 source, Expression`1 predicate)
   at DataAccessLayer.UserRepository.FetchEmployeeDetails(String empid) in C:\Users\user\OneDrive - jdbots\Visual Studio\EFDemo\DataAccessLayer\UserRepository.cs:line 21
   at ConsoleApp.Program.Main(String[] args) in C:\Users\user\OneDrive - jdbots\Visual Studio\EFDemo\ConsoleApp\Program.cs:line 12

The error stack might be somewhat different for you based on your implementation but the solution will be the same. Right-click on the appsettings.json file and click on Properties. This solution uses Visual Studio.

Check the property Copy to Output Directory. It might be Do not copy. Change this to Copy always.

Thank you All!!! Hope you find this useful.


Leave a Reply

Up ↑

Discover more from JD Bots

Subscribe now to keep reading and get access to the full archive.

Continue reading