在我的aspnetcore应用(v2.1)中,我需要配置一个〜/ wwwroot / App_Data / quranx.db中的只读数据库(entityframework核心+ SQLite)
我需要在Startup.ConfigureServices中调用此代码
services.AddDbContext<QuranXDataContext>(options => options
.UseSqlite($"Data Source={databasePath}")
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
);
但是到那时,我找不到找到wwwroot路径的方法。要获得该路径,我需要IhostingEnvironment,但是直到Startup.Configure被调用,并且Startup.ConfigureServices完成之后,我才能获得对此的引用。
这是怎么做的?
答案 0 :(得分:4)
在IHostingEnvironment
中访问ConfigureServices
很容易(我已经在下面解释了如何),但是在阅读详细信息之前,请先看一下Chris Pratt在有关如何存储数据库的注释中的警告。 wwwroot是一个非常坏主意。
您可以在IHostingEnviroment
类中采用Startup
类型的构造函数参数,并将其捕获为字段,然后可以在ConfigureServices
中使用该字段:
public class Startup
{
private readonly IHostingEnvironment _env;
public Startup(IHostingEnvironment env)
{
_env = env;
}
public void ConfigureServices(IServiceCollection services)
{
// Use _env.WebRootPath here.
}
// ...
}
答案 1 :(得分:0)
Path.GetFullPath("wwwroot");