CleanArchitecture-Template icon indicating copy to clipboard operation
CleanArchitecture-Template copied to clipboard

System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found

Open daliri72 opened this issue 3 years ago • 6 comments

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

daliri72 avatar Jul 01 '22 18:07 daliri72

Write this command in powershell on Persistance directory can create database. dotnet ef --startup-project ../../Web/Api database update --context AppDbContext

daliri72 avatar Jul 01 '22 18:07 daliri72

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.

omid-ahmadpour avatar Jul 02 '22 06:07 omid-ahmadpour

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.

Mjceaser1 avatar Aug 20 '22 06:08 Mjceaser1

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.

Mjceaser1 avatar Aug 20 '22 16:08 Mjceaser1

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 avatar Aug 31 '22 12:08 akazemie

@akazemie Thanks for your suggestion. Please fork the project and make changes, then I can merge it with the repository.

omid-ahmadpour avatar Aug 31 '22 13:08 omid-ahmadpour

As there have been no further responses on this issue, it will now be closed.

omid-ahmadpour avatar Dec 24 '23 12:12 omid-ahmadpour