Entity framework core IHostingEnvironment如何传递模型创建?

Entity framework core IHostingEnvironment如何传递模型创建?,entity-framework-core,asp.net-core-2.1,Entity Framework Core,Asp.net Core 2.1,我想创建一些从json文件加载的虚拟数据。我需要获取文件的路径,根据我的研究,我发现我需要IHostingEnvironment,但我不确定如何在我的DbContext类文件中获取属性。如果要使用外部“初始值设定项”类(至少在ASP.NET核心应用程序的上下文中),您可能会这样做: DbContext类: public class SampleDbContext : Microsoft.EntityFrameworkCore.DbContext { public SampleDbCont

我想创建一些从json文件加载的虚拟数据。我需要获取文件的路径,根据我的研究,我发现我需要IHostingEnvironment,但我不确定如何在我的DbContext类文件中获取属性。

如果要使用外部“初始值设定项”类(至少在ASP.NET核心应用程序的上下文中),您可能会这样做:

DbContext类:

public class SampleDbContext : Microsoft.EntityFrameworkCore.DbContext {
    public SampleDbContext(DbContextOptions<SampleDbContext> options) : base(options) { }

    public DbSet<SampleRecord> SampleRecords { get; set; }

    //...Other DB structure here

    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        //...
    }
}
Startup.cs:

public class Startup {

    public IHostingEnvironment HostingEnvironment { get; }

    public Startup(IHostingEnvironment env) {
        //...

        HostingEnvironment = env;

        //...
    }

    public void ConfigureServices(IServiceCollection services) {
        //...

        services.AddDbContext<SampleDbContext>(/*Your options here*/);

        services.AddSingleton<IHostingEnvironment>(HostingEnvironment);

        //...
    }

    //... Rest of class
}
公共类启动{
公共IHostingEnvironment主机环境{get;}
公共启动(IHostingEnvironment环境){
//...
HostingEnvironment=env;
//...
}
public void配置服务(IServiceCollection服务){
//...
services.AddDbContext(/*此处的选项*/);
AddSingleton(主机环境);
//...
}
//…其他同学
}
Program.cs:

public class Program {
    public static void Main(string[] args) {
        var host = CreateWebHostBuilder(args).Build()

        using (var scope = host.Services.CreateScope()) {
            var services = scope.ServiceProvider;
            var hostingEnvironment = services.GetService<IHostingEnvironment>();

            //... do whatever you need to get your JSON file here (var jsonData = ...)

            var sampleDbContext = services.GetService<SampleDbContext>();
            Initializer.Initialize(sampleDbContext, jsonData);
        }

        host.Run();
    }

    //... Rest of class
}
公共类程序{
公共静态void Main(字符串[]args){
var host=CreateWebHostBuilder(args).Build()
使用(var scope=host.Services.CreateScope()){
var services=scope.ServiceProvider;
var hostingEnvironment=services.GetService();
//…在这里获取JSON文件需要做什么(var jsonData=…)
var sampleDbContext=services.GetService();
Initializer.Initialize(sampleDbContext,jsonData);
}
host.Run();
}
//…其他同学
}

种子数据将与模式分开,并在应用程序运行时加载到数据库中。您也可以从测试代码中调用初始值设定项(如果您想将特定的测试数据放入测试中,也可以不调用)。

您是否可以使用外部“初始值设定项”,而不是将数据种子直接添加到上下文中(并将模式与数据混合)类类似于这里的示例:无论如何我都可以这样做,这是最佳实践,允许我访问文件路径。我查看了链接,但没有看到serviceProvider.GetRequiredService();当我尝试使用IServiceProviderI运行.net core 2.1时,我似乎没有host.Services.CreateScope()是的,看起来2.1使用的是IWebHostBuilder而不是IWebHost,我一定在标记上读取了错误的版本。看起来您可以使用IWebHostBuilder.Build()获取IWebHost的实例,因此可以执行IWebHostBuilder.Build().Services.CreateScope()?不,生成有服务,但没有CreateScope。您可能需要导入
public class Program {
    public static void Main(string[] args) {
        var host = CreateWebHostBuilder(args).Build()

        using (var scope = host.Services.CreateScope()) {
            var services = scope.ServiceProvider;
            var hostingEnvironment = services.GetService<IHostingEnvironment>();

            //... do whatever you need to get your JSON file here (var jsonData = ...)

            var sampleDbContext = services.GetService<SampleDbContext>();
            Initializer.Initialize(sampleDbContext, jsonData);
        }

        host.Run();
    }

    //... Rest of class
}