[英]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.