How to get ApplicationDbContext instance in AccountController for ASP .NET Core 2.0 With Individual Authentication? c# dependency-injection entity-framework-core


I'm newbie in ASP .NET Core and need little help. I have created ASP .NET Core 2.0 MVC project with individual authentication. Now I want to access Current ApplicationDbContext in my AccountController. By searching i found that we can use Dependency Injection like this code.

    private readonly ApplicationDbContext _context;

    public AccountController(ApplicationDbContext context)
        _context = context;

This method works good in all other controllers except Scaffolded AccountController and the following error is generated on running project:

InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'PacePreneur.Controllers.AccountController'. There should only be one applicable constructor.

I know this error is due to this new constructor as AccounntController already has a constructor but i don't know how to use that for getting current DbContext. I'm still searching for a other solutions.

PS: Sorry if it's a non-sense question.

7/13/2018 12:45:38 PM

Accepted Answer

Make only one constructor and make sure that that constructor takes in everything that your first constructor did but add the ApplicationDbContext as another parameter. So if your original constructor took in params a, b, remove the new constructor and add the dbcontext to constructor that takes a, b to now be a, b, dbcontext –

7/13/2018 2:21:37 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow