CleanArchitecture-Template
CleanArchitecture-Template copied to clipboard
System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found
When I set default project to Persistence and then Update-Database -Context AppDbContext in Package Manager Console and then show this error. How can I solve this problem?
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional. The expected physical path was 'D:\sample\src\Infrastructure\Persistance\bin\Debug\net6.0\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(IList1 providers) at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build() at Sample.Persistance.DesignTimeDbContextFactoryBase1.Create(String basePath, String environmentName) in D:\sample\src\Infrastructure\Persistance\DesignTimeDbContextFactoryBase.cs:line 26 at Sample.Persistance.DesignTimeDbContextFactoryBase1.CreateDbContext(String[] args) in D:\sample\src\Infrastructure\Persistance\DesignTimeDbContextFactoryBase.cs:line 18
Write this command in powershell on Persistance directory can create database.
dotnet ef --startup-project ../../Web/Api database update --context AppDbContext
Please make sure your project build successfully. After that set the Api layer as your startup project. If your problem still exist, feel free to contact me.
I ran into this issue also. I resolved it by ensuring that the 'Private Assets' value was cleared instead of being set to 'All' for each Microsoft.EntityFramework dependency package.
I tested again with a fresh solution.
The 3 packages that have 'Private Assets' set to 'All' are: - Persistance Package: Microsoft.EntityFrameworkCore.Design (6.0.0) - Persistance Package: Microsoft.EntityFrameworkCore.Tools (6.0.0) - ApiFramework Package: Microsoft.EntityFrameworkCore.Design (6.0.0)
I cleared the 'Private Assets' value for ApiFramework Package: Microsoft.EntityFrameworkCore.Design (6.0.0) only. I set the startup project to Api. I was then able to successfully run the update command: Update-Database -Context AppDbContext.
Please change the Create method in DesignTimeDbContextFactoryBase.cs file as below:
private TContext Create(string basePath, string environmentName)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(basePath)
.AddJsonFile("appsettings.json")
.AddJsonFile(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),
$"appsettings.{environmentName}.json"), optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var appOptions = configuration.GetSection("AppOptions:WriteDatabaseConnectionString");
var connectionString = appOptions.Value;
return Create(connectionString);
}
@akazemie Thanks for your suggestion. Please fork the project and make changes, then I can merge it with the repository.
As there have been no further responses on this issue, it will now be closed.