How to add DbContext Dependency Injection in .NET Core
๐ก ๐๐จ๐ฐ ๐ญ๐จ ๐๐๐ ๐๐๐๐จ๐ง๐ญ๐๐ฑ๐ญ ๐๐๐ฉ๐๐ง๐๐๐ง๐๐ฒ ๐๐ง๐ฃ๐๐๐ญ๐ข๐จ๐ง ๐ข๐ง .๐๐๐ ๐๐จ๐ซ๐ ๐๐๐ ?
๐ก ๐๐จ๐ฐ ๐ญ๐จ ๐๐๐ ๐๐๐๐จ๐ง๐ญ๐๐ฑ๐ญ ๐๐๐ฉ๐๐ง๐๐๐ง๐๐ฒ ๐๐ง๐ฃ๐๐๐ญ๐ข๐จ๐ง ๐ข๐งย .๐๐๐ ๐๐จ๐ซ๐ ๐๐๐ย ?
๐ฏ ๐๐ญ๐๐ฉ ๐ย : Define a DbContext class that represents the database context in your application.
๐ฏ ๐๐ญ๐๐ฉ ๐ย : In the ConfigureServices/Program.cs add a call to the AddDbContext method to register the DbContext with the dependency injection container.
๐ฏ ๐๐ญ๐๐ฉ ๐ย : Inject the DbContext instance into the constructor of your controllers or services that require it or maybe you can use Unit of work class as wellย , its up to youย :)
If you like my work you can help me in growing through following ways
Subscribe my Weeklyย .NET Newsletter where I would be publishing one tip on C#/.NET on weekly basis.
You can Buy a Coffee for me.
#dotnetcore #dotnet #csharp #dependency #dependencyinjection