如果您不在ASP.NET 5中,EF7如何引导依赖项注入(IServiceCollection)? [英] How does EF7 bootstrap dependency injection (IServiceCollection) if you're not in ASP.NET 5?

查看:180
本文介绍了如果您不在ASP.NET 5中,EF7如何引导依赖项注入(IServiceCollection)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在努力弄清ASP.NET 5/EF 7的功能.我正在使用DNX项目(.xproj).

I'm still trying to get my head around what's what with ASP.NET 5 / EF 7. I'm using DNX projects (.xproj).

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

Startup is used by OWIN/ASP.NET for configuring, loading services, etc. But it's also used for EF 7 migrations (to set your DbContextOptions for example).

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

My main goal is to know how EF7 (and ASP.NET 5) bootstrap with Startup and who's creating the startup class, initializing the DI container, etc.

就上下文而言,我需要做的一个例子是,在我的xUnit单元测试(位于它们自己的程序集中,并引用没有Startup类的数据程序集)中,我需要AddDbContext设置我的连接.

An example of what I need to do, for context, is that in my xUnit unit tests (which are in their own assembly and reference my data assembly which doesn't have a Startup class), I need to AddDbContext to set my connection.

我有示例启动类:

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)
        {
        }
    }
}

这当前在我的数据程序集中,而不在我的单元测试程序中.我尝试添加应用设置(我知道是OWIN,但我想尝试一下):

This is currently in my data assembly and not my unit test assembly. I tried adding the app setting (I know it's OWIN but I thought I'd give it a shot):

<appSettings>
    <add key="owin:appStartup" value="Radar.Data.Startup, Radar.Data" />
</appSettings>

启动类未得到执行.

我真的很想了解Startup的总体机制,调用它的人,等等,但是现在,我只需要了解EF 7如何初始化依赖项/服务,以便可以正确地初始化我的单元测试.

I'd really like an understanding of the overall mechanism with Startup, who calls it, etc., but for now, I just need an understanding of how EF 7 initializes dependencies/services so that I can properly initialize my unit tests.

更新 到目前为止,这是我在单元测试中得到的,并且我认为我可以使它在某一时刻起作用:

UPDATE Here's what I've got in my unit test so far and I thought I had it working at one point:

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"));

但是现在实例化我的DbContext时,我得到的是No service for type 'Microsoft.Data.Entity.Internal.IDbSetInitializer' has been registered.因此,显然没有加载所有EF服务.

but now I'm getting No service for type 'Microsoft.Data.Entity.Internal.IDbSetInitializer' has been registered when my DbContext is instantiated. So obviously not getting all the EF services loaded.

如果我将其注释掉:

DbContextActivator.ServiceProvider = serviceProvider;

它先前的错误为:No database providers are configured. Configure a database provider by overriding OnConfiguring in your DbContext class or in the AddDbContext method when setting up services.

设置DbContextActivator.ServiceProvider是EF7中唯一可以找到设置您自己的提供程序的钩子的位置.我也很高兴获得EF7内部服务集合的实例并与之合作.我想我将再次搜索EF7单元测试代码,以查看是否缺少关键部分.

Setting DbContextActivator.ServiceProvider is the only place in EF7 where I can find a hook to set your own provider. I'd be just as happy getting an instance of EF7's internal service collection and working with that. I think I'm going to scour the EF7 unit test code again and see if I'm missing a critical piece.

推荐答案

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

Startup class is created by Microsoft.AspNet.Hosting package when you run you web application (see StartupLoader.cs).

您还可以查看WebApplication.Run方法( WebApplication.Run )是ASP.NET 5 Web应用程序的入口点.

You can also look onto WebApplication.Run method (WebApplication.Run) its an entry point to ASP.NET 5 web applications.

DI在WebHostBuilder类中初始化(

DI is initialized in WebHostBuilder class (WebHostBuilder.cs) and inside dnx in Bootstrapper class (Bootstrapper.cs)

这篇关于如果您不在ASP.NET 5中,EF7如何引导依赖项注入(IServiceCollection)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆