Dependency injection 如果您';你不在ASP.NET 5中吗?

Dependency injection 如果您';你不在ASP.NET 5中吗?,dependency-injection,asp.net-core,entity-framework-core,Dependency Injection,Asp.net Core,Entity Framework Core,我仍在试图弄清楚ASP.NET5/EF7到底是什么。我正在使用DNX项目(.xproj) OWIN/ASP.NET使用Startup配置、加载服务等,但它也用于EF 7迁移(例如设置DbContextOptions) 我的主要目标是了解EF7(和ASP.NET 5)如何使用Startup引导,以及谁在创建Startup类、初始化DI容器等等 例如,我需要做的一个例子是,在我的xUnit单元测试中(它们在自己的程序集中,并引用我的数据程序集,该程序集没有启动类),我需要AddDbContext来设

我仍在试图弄清楚ASP.NET5/EF7到底是什么。我正在使用DNX项目(.xproj)

OWIN/ASP.NET使用
Startup
配置、加载服务等,但它也用于EF 7迁移(例如设置
DbContextOptions

我的主要目标是了解EF7(和ASP.NET 5)如何使用
Startup
引导,以及谁在创建Startup类、初始化DI容器等等

例如,我需要做的一个例子是,在我的xUnit单元测试中(它们在自己的程序集中,并引用我的数据程序集,该程序集没有
启动
类),我需要
AddDbContext
来设置连接

我有一个示例startup类:

namespace Radar.Data
{
    using Microsoft.AspNet.Builder;
    using Microsoft.AspNet.Hosting;
    using Microsoft.Data.Entity;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.PlatformAbstractions;

    public class Startup
    {
        public IConfigurationRoot Configuration { get; set; }

        public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(appEnv.ApplicationBasePath)
                .AddJsonFile("appsettings.json");

            Configuration = builder.Build();
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddEntityFramework()
                .AddSqlServer()
                .AddDbContext<RadarDbContext>(options =>
                    options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
        }

        public void Configure(IApplicationBuilder app)
        {
        }
    }
}
但是现在,当我的
DbContext
被实例化时,没有注册类型为“Microsoft.Data.Entity.Internal.IDbSetInitializer”的服务。所以显然没有加载所有的EF服务

如果我不这样评论:

DbContextActivator.ServiceProvider = serviceProvider;
前面的错误是:
未配置数据库提供程序。设置服务时,通过覆盖DbContext类或AddDbContext方法中的OnConfiguration来配置数据库提供程序。


设置
DbContextActivator.ServiceProvider
是EF7中唯一可以找到钩子来设置您自己的提供者的地方。我会很高兴得到一个EF7内部服务集合的实例,并与之合作。我想我要再次浏览一下EF7单元测试代码,看看是否遗漏了一个关键部分。

Startup
类是由
Microsoft.AspNet.Hosting
包在运行web应用程序时创建的(请参阅)

您还可以查看
WebApplication.Run
method(),它是ASP.NET 5 web应用程序的入口点


DI在
WebHostBuilder
class()中初始化,在
Bootstrapper
class()中的
dnx
中初始化。通过查看EF Identity Framework library@JoeAudette中的测试,您可能会获得一些见解,谢谢。看来我还有点学习要做哈哈!谢谢这些都是来源中非常有用的观点。例如,在通过xUnit.runner.dnx启动的xUnit测试中,我如何访问DI容器,以便获得IServiceCollection并实例化和调用EntityFrameworkServicesBuilder.AddDbContext来指定我的连接字符串?启动似乎不是dnx的一部分,而是ASP.NET的一部分(不知何故EF迁移侵入了它?)是的,启动是ASP.NET概念,您可以通过
services.AddEntityFramework()
call()添加EF服务。是的,我遇到了这个问题,请查看我的更新问题。
ServiceCollection serviceCollection = new ServiceCollection();
IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
DbContextActivator.ServiceProvider = serviceProvider;
serviceCollection.AddEntityFramework()
    .AddSqlServer()
    .AddDbContext<RadarDbContext>(
    options => options.UseSqlServer("Server=.;Database=SonOfRadar;Trusted_Connection=True;MultipleActiveResultSets=True"));
DbContextActivator.ServiceProvider = serviceProvider;