简体   繁体   English

将 DbContext 解析为派生类的 ASP.NET MVC 问题

[英]ASP.NET MVC issue resolving DbContext to a Derived class

I do have a ASP.NET Core 2.0 application with Microsoft.Extensions.DependencyInjection .我确实有一个带有Microsoft.Extensions.DependencyInjection的 ASP.NET Core 2.0 应用程序。

I added my Context ApplicationDbContext to the Startup.cs file:我将我的 Context ApplicationDbContext添加到Startup.cs文件中:

services.AddDbContext<ApplicationDbContext>(options => 
    options.UseSqlServer(DefaultConnection));

My ApplicationDbContext inherits the DbContext class:我的ApplicationDbContext继承了DbContext类:

In a generic controller I require a DbContext class:在通用控制器中,我需要一个DbContext类:

public GenericController(DbContext context){

However Microsoft.Extensions.DependencyInjection cannot resolve the DbContext to the ApplicationDbContext registered service.但是Microsoft.Extensions.DependencyInjection无法将DbContext解析为ApplicationDbContext注册服务。

How can I inject the required DbContext class into my service class?如何将所需的DbContext类注入到我的服务类中?

I'm able to change the GenericController , but the project where GenericController lies does not have reference to the ApplicationDbContext .我可以更改GenericController ,但是GenericController所在的项目没有对ApplicationDbContext引用。

I'm developing a middleware, it should require DbContext that should be specialized in other projects.我正在开发一个中间件,它应该需要专门用于其他项目的 DbContext。 So the controller is in the middleware, it does not have a reference to the specialized class.所以控制器在中间件中,它没有对专用类的引用。

the project where GenericController lies does not have reference to the ApplicationDbContext GenericController 所在的项目没有对ApplicationDbContext引用

This is 100% of your issue.这是您问题的 100%。

You either need to ensure that ApplicationDbContext is in a project that the services that use it can reference:您要么需要确保ApplicationDbContext位于使用它的服务可以引用的项目中:

public GenericController(ApplicationDbContext context){

or you need to create an IApplicationDbContext that is implemented by ApplicationDbContext that can be referenced from the services that use it:或者您需要创建一个由ApplicationDbContext实现的IApplicationDbContext可以从使用它的服务中引用:

public GenericController(IApplicationDbContext context){

Injecting DbContext alone isn't going to cut it because it doesn't expose any of the tables that your application needs to use.单独注入DbContext不会削减它,因为它不会公开您的应用程序需要使用的任何表。

I solved it by Just adding a Transient from derived class to parent class below the AddContext command我通过在AddContext命令下面添加一个 Transient 从派生类到父类来解决它

services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(DefaultConnection));
services.AddTransient<DIADbContext,ApplicationDbContext>();

Another solution can be to register the DbContext with a custom factory which will return the already registered ApplicationDbContext instance from the container:另一种解决方案是使用自定义工厂注册DbContext ,该工厂将从容器中返回已注册的ApplicationDbContext实例:

services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(DefaultConnection));
services.AddScoped<DbContext, ApplicationDbContext>(s => s.GetService<ApplicationDbContext>());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM