1

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.

5
  • 1
    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 Commented Jul 13, 2018 at 12:49
  • Amazing.. It worked. Really thankful :) Commented Jul 13, 2018 at 12:56
  • You're welcome. I'm going to put my comment as an answer per Stack Overflow's guidelines, feel free to mark it as the accepted answer if happy with it. Commented Jul 13, 2018 at 12:59
  • I have accepted your answer. Commented Jul 13, 2018 at 13:01
  • Thank you. Have a good day. Commented Jul 13, 2018 at 13:36

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.