为什么 EF7 说我没有配置任何提供程序,而 ASP.net Identity 在相同的 DbContext 下工作正常?

Why does EF7 say I haven't configured any providers, when ASP.net Identity is working fine with the same DbContext?

我刚刚开始使用 ASP.net 5、MVC6 和 Entity Framework 7 进行实验项目。我的 ASP.Net Identity 工作正常,但随后尝试添加一些我自己的数据到 DbContext 并遇到了这个问题。 EF7 报告:

An unhandled exception occurred while processing the request.

InvalidOperationException: 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.

Microsoft.Data.Entity.Internal.DatabaseProviderSelector.SelectServices(ServiceProviderSource providerSource) Stack Query Cookies Headers

InvalidOperationException: 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.

这是我的配置方法:

    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddApplicationInsightsTelemetry(Configuration);

        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.AddMvc();

        // Add application services.
        services.AddTransient<IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();

        services.AddTransient<ApplicationUnitOfWork>(instance => new ApplicationUnitOfWork());
    }

ApplicationUnitOfWork 是 EF7 的外观,用于减少紧耦合。这是到目前为止的全部内容:

public class ApplicationUnitOfWork : IUnitOfWork
    {
    readonly ApplicationDbContext dbContext;

    public ApplicationUnitOfWork()
        {
        dbContext = new ApplicationDbContext();
        Products = new ProductRepository(dbContext);
        }

    public void Dispose() { dbContext.Dispose(); }

    public IUserRepository Users { get; }

    public IRepository<SoftwareProduct> Products { get; }

    public async Task CommitAsync() { await dbContext.SaveChangesAsync(); }

    public void Cancel() { throw new NotImplementedException(); }
    }

现在,当我 运行 Web 应用程序时,我可以注册用户帐户并登录,然后 EF 创建数据库、创建身份 table 并使用用户数据填充它们。产品 table 也已创建 - 显然在某种程度上 EF 能够使用 ApplicationDbContext 并找到用于创建数据库和模式的提供程序。

但是,当我尝试访问使用 ProductsRepository 的 Products 控制器时,EF 会抱怨 - 尽管在这两种情况下都使用了 ApplicationDbContext 并用于创建数据库!

那怎么会呢? ASP.net Identity 可以通过某种方式获得提供者,而我自己的代码却不能,这有什么特别之处? "magic incantation" 我错过了什么?

这是因为您正在更新它而不是为您注入它。您要更新的那个还没有配置。 您应该更改 class 以将其传递给构造函数。

DbContext 有多个构造函数,而您正在使用未配置它的空构造函数。

最好通过这样构造构造函数来注入它:

 public ApplicationUnitOfWork(ApplicationDbContext context)
 {
    dbContext = context;
    Products = new ProductRepository(dbContext);
 }

你的代码显示 ApplicationDbContext 已经注册到 DI,Identity 正在使用注入的,但你不是,因为你自己用无参数构造函数更新了它

您还应该注册您的 ApplicationUnitOfWork 以便它可以被注入:

services.AddScoped<ApplicationUnitOfWork, ApplicationUnitOfWork>();