My EF project is in one project and my ASP.NET Core project in another project ,
D:\AspProjects\DatabaseHafez> <=== my ef model is in this folder
D:\AspProjects\Hafez> <=== my aspnet core 3 is in this folder
so each project has one bin folder.
The below builder(ConfigurationBuilder) should have the path of appsettings.json file for reading connections string.so the below has this path =>
D:\AspProjects\DatabaseHafez\bin\Debug\netcoreapp3.1\appsettings.json
but my appsettins.json file in my asp.net core project so after bulilding is will copy to output folder =>
D:\AspProjects\Hafez\bin\Debug\netcoreapp3.1\appsettings.json
so How can i find the output folder path?
public class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
AppDbContext IDesignTimeDbContextFactory<AppDbContext>.CreateDbContext(string[] args)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
IConfigurationRoot configuration = builder.Build();
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
optionsBuilder.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
var context = new AppDbContext(optionsBuilder.Options);
context.Database.EnsureCreated();
return context;
}
}
Now I want to add migrations But I get an error
D:\AspProjects\DatabaseHafez>dotnet ef migrations add changed98112601
Build started...
Build succeeded.
System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'D:\AspProjects\DatabaseHafez\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(IList1 providers)
1 factory)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at DatabaseHafez.AppDbContextFactory.Microsoft.EntityFrameworkCore.Design.IDesignTimeDbContextFactory<DatabaseHafez.AppDbContext>.CreateDbContext(String[] args) in D:\AspProjects\DatabaseHafez\AppDbContextFactory.cs:line 27
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContextFromFactory(Type factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_1.<FindContextTypes>b__9()
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)The configuration file 'appsettings.json' was not found and is not optional. The physical path is 'D:\AspProjects\DatabaseHafez\bin\Debug\netcoreapp3.1\appsettings.json'.
Now I want to know how can I find the bin
folder of my ASP.NET Core project?
The Bin folder is used for managed-code assemblies, not for native-code (unmanaged-code) assemblies. For more information, see Loading C++ Assemblies in ASP.Net. In ASP.NET 2.0 and later versions, you can put strong-named (signed) assemblies in the Bin folder.
Try to get it line this:
var runDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
GetCurrentDirectory
might also work, but there is this bug here: https://github.com/dotnet/project-system/issues/589 so I would avoid it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With