2

I do have a ASP.NET Core 2.0 application with Microsoft.Extensions.DependencyInjection.

I added my Context ApplicationDbContext to the Startup.cs file:

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

My ApplicationDbContext inherits the DbContext class:

In a generic controller I require a DbContext class:

public GenericController(DbContext context){

However Microsoft.Extensions.DependencyInjection cannot resolve the DbContext to the ApplicationDbContext registered service.

How can I inject the required DbContext class into my service class?

I'm able to change the GenericController, but the project where GenericController lies does not have reference to the ApplicationDbContext.

I'm developing a middleware, it should require DbContext that should be specialized in other projects. So the controller is in the middleware, it does not have a reference to the specialized class.

0

3 Answers 3

2

the project where GenericController lies does not have reference to the ApplicationDbContext

This is 100% of your issue.

You either need to ensure that ApplicationDbContext is in a project that the services that use it can reference:

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:

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.

Sign up to request clarification or add additional context in comments.

2 Comments

unfortunately I'm developing a middleware, it should require a specific DbContext that should be specialized in other projects. So the controller is in the middleware, it does not have a reference to the specialized class. only to the parent one. I think that i could resolve child classes.
If it doesn't have a reference to the concrete ApplicationDbContext then you will need to make a IApplicationDbContext interface in an assembly that is shared between the middleware assembly and the assembly that defines ApplicationDbContext.
1

I solved it by Just adding a Transient from derived class to parent class below the AddContext command

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

Comments

1

Another solution can be to register the DbContext with a custom factory which will return the already registered ApplicationDbContext instance from the container:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.