如何從ASP.NET Core中的DbContext獲取IHostingEnvironment

asp.net-core c# entity-framework entity-framework-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; }
}

如果我在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; }
}

從Package-Manager控制台添加遷移時出現以下錯誤。

PM> Add-Migration InitMyDBContext

在'MyDBContext'上找不到無參數構造函數。要么將無參數構造函數添加到'MyDBContext',要么在與'MyDBContext'相同的程序集中添加'IDbContextFactory'的實現。

熱門答案

您應該能夠將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; }
}

該框架已經了解了該接口。

您可以照常添加上下文。減負配置,因為您表示您不想在Startup執行此操作

services.AddDbContext<MyDBContext>();

在初始化上下文時,框架應該根據構造函數參數的存在將IHostingEnvironment的實現注入上下文中。



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow