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.cs
file, in theConfigureServices
method, 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 aMyContext
instance. By doing so, I want thatMyController
can get injected an instance ofDAO
instead of getting injectedMyContext
directly , and I am aiming to inject an instance ofMyContext
intoDAO
. In other words; The instance ofDAO
should use/receive the instanced ofMyContext
, andMyController
should receive the instance ofDAO
.Then, the constructor of
MyController
should look like something like this:And my
DAO
class would look like something like thisHow can I achieve this? What methods do I need to use in
Startup.ConfigureServices
to do this?Beta Was this translation helpful? Give feedback.
All reactions