Replies: 1 comment
-
| You have to tell the container about your DataAccessObject: services.AddTransient<DataAccessObject>();Read more about dependency injection here https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.1 | 
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
Uh oh!
There was an error while loading. Please reload this page.
-
Hi!
I am working on a API and want to utilise the Dependency Injection functionality that is built into ASP.NET Core. However, I am struggling to solve a seemingly simple problem. I am using Entity Framework Core to connect with my MySQL database, and unfortunately, I couldn't exactly find a possible answer to my question.
So I have configured a DbContext.
And then in the
Startup.csfile, in theConfigureServicesmethod, I haveNow in my Controller, I have
Now the instance of MyContext is injected into my controller just fine and it all works.
For testing purposes, I have introduced a new class
DAO, which uses aMyContextinstance. By doing so, I want thatMyControllercan get injected an instance ofDAOinstead of getting injectedMyContextdirectly , and I am aiming to inject an instance ofMyContextintoDAO. In other words; The instance ofDAOshould use/receive the instanced ofMyContext, andMyControllershould receive the instance ofDAO.Then, the constructor of
MyControllershould look like something like this:And my
DAOclass would look like something like thisHow can I achieve this? What methods do I need to use in
Startup.ConfigureServicesto do this?Beta Was this translation helpful? Give feedback.
All reactions