I am trying all day to figure out to get the ApplicationDbContext
in the ManageController.cs
of a default MVC 6 project.
I went online and Googled a lot but no one seems to have the same problem as I have with it. It is probably simple but I can't figure it out. Anyone has an idea?
Here is what I tried:
IServiceProvider service = new IServiceProvider();
var _context = service.GetService<ApplicationDbContext>();
I am using Visual Studio 2015 Update 3. Some of the following steps might not be needed in the future release of Visual Studio.
Create ASP.NET Core (with .NET Core) project with
No Authentification
.In Package Manager Console, execute each of the following, one after the other.
"tools":{}
defined inproject.json
.appsettings.json
.ConfigureServices
instartup.cs
right beforeservices.AddMvc();
.Use constructor injection:
Then you can use the
_context
object in your controller methods. There's more info in the Dependency Injection section of the docs.