4

I have two different controllers.

One is a came by default ASP.NET MVC Core :

public ManageController(
  UserManager<ApplicationUser> userManager,
  SignInManager<ApplicationUser> signInManager,
  IEmailSender emailSender,
  ILogger<ManageController> logger,
  UrlEncoder urlEncoder)
{
    _userManager = userManager;
    _signInManager = signInManager;
    _emailSender = emailSender;
    _logger = logger;
    _urlEncoder = urlEncoder;
}

and my own that I made with scaffolding:

public CarsController(CarsContext context)
{
    _context = context;
}

.

What I want to achieve: I'd like to use my CarsContext in one of ManageController's action method, but I don't know how could I instatiate, because CarsContext constructor looks like this:

public CarsContext(DbContextOptions<CarsContext> options)
    : base(options)
{
}

. I don't know what could I add to constructor from a method in ManageController.

The task I'd like to achieve is to get cars from my CarsContext, to display those.

My another idea is to call the index method from my CarsController in method of ManageController, but also I don't know how to get it.

3
  • 1
    Can you make CarsController inherit ManageController? Commented May 19, 2018 at 9:56
  • There is a lot of paramters in ManageController's ctor, I don't really know where it gets, because if I check it references in VisualStudio there is no reference for it. Commented May 19, 2018 at 10:11
  • See : code.msdn.microsoft.com/ASPNET-MVC-5-Security-And-44cbdb97/… Commented May 19, 2018 at 10:31

1 Answer 1

2

For this you can inject CarsContext to ManageController as you are injecting in CarsController and dependency injection framework will take care of rest

But do register CarsContext in dependency injection framework.

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

1 Comment

Thanks your answer lead me to right solution. I have already registered my DbContext in dependency injection framework before, so I only needed to add the required class(CarsContext) to my constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.