如何从 ASP.NET Core 中的 DbContext 获取 IHostingEnvironment

Zin*_*Min 1 c# entity-framework entity-framework-core asp.net-core

只是为了测试,我不想从 StartUp.cs 文件进行依赖注入。如何从 EF Core DBContext 获取 IHostingEnvironment。

我采用了一个带有空模板的新 asp.net 核心项目。我创建了一个 dbcontext,如下所示。我想使用 Environment.ContentRootPath 而不是 Directory.GetCurrentDirectory()。但我不想从 Startup.cs 做任何注入。

public class MyDBContext: DbContext
{

    public IConfigurationRoot Configuration { get; set; }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var builder = new ConfigurationBuilder()
              .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "..\\..\\.."))
              .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
        Configuration = builder.Build();
        optionsBuilder.UseSqlServer(Configuration.GetConnectionString("SQLCN"));// @"Server=(localdb)\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;");
    }

    public DbSet<Teacher> Teachers { get; set; }
    public DbSet<Lesson> Lessons { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

如果我在 dbcontext 构造函数中添加 IHostingEnvironment 如下,

public class MyDBContext: DbContext
{
    private readonly IHostingEnvironment env;
    public MyDBContext(IHostingEnvironment env) : base()
    {
        this.env = env;
    }


    public IConfigurationRoot Configuration { get; set; }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        var builder = new ConfigurationBuilder()
              .SetBasePath(this.env.ContentRootPath)
              .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
        Configuration = builder.Build();
        optionsBuilder.UseSqlServer(Configuration.GetConnectionString("SQLCN"));// @"Server=(localdb)\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;");
    }

    public DbSet<Teacher> Teachers { get; set; }
    public DbSet<Lesson> Lessons { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

当我从包管理器控制台添加迁移时出现以下错误。

PM> Add-Migration InitMyDBContext
Run Code Online (Sandbox Code Playgroud)

在“MyDBContext”上找不到无参数构造函数。向“MyDBContext”添加无参数构造函数,或在与“MyDBContext”相同的程序集中添加“IDbContextFactory”的实现。

Nko*_*osi 5

您应该能够将IHostingEnvironment直接注入到 DbContext 的构造函数中。

public class MyDBContext: DbContext {
    private readonly IHostingEnvironment env;
    public MyDBContext(IHostingEnvironment env) : base() {
        this.env = env;
    }

    public IConfigurationRoot Configuration { get; set; }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
        Configuration = builder.Build();
        optionsBuilder.UseSqlServer(Configuration.GetConnectionString("SQLCN"));// @"Server=(localdb)\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;");
    }

    public DbSet<Teacher> Teachers { get; set; }
    public DbSet<Lesson> Lessons { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

该框架已经知道该接口。

您像往常一样添加上下文。减去配置,因为您表示您不想在Startup

services.AddDbContext<MyDBContext>();
Run Code Online (Sandbox Code Playgroud)

在初始化上下文时,框架应该IHostingEnvironment根据构造函数参数的存在将 的实现注入到上下文中。