Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8304487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:51:40+00:00 2026-06-08T17:51:40+00:00

Right now I have three controllers. Random (the parent), Menu & Name (the children).

  • 0

Right now I have three controllers. Random (the parent), Menu & Name (the children).

I have several methods in RandomController that work on the database, but I want Menu & Name to specify different databases. However, if I remove the database context declaration from Random, it throws all sorts of errors.

As a side note, Random will never be accessed on its own, it only exists to provide code for Menu & Name

The entire controller isn’t really necessary, but here are some of the methods I have to give an idea. All of the db. statements break when I move the ComboContext declaration to the children.

public class RandomController : Controller
{

    publicCombosContext db = new CombosContext();
    //
    // GET: /Home/

    public ActionResult Index()
    {
        var rows = db.Combos.OrderBy(a => a.Id).ToArray();
        int arrLength = rows.Length;
        Random ran = new Random();

        Combo newCombo = new Combo
        {
            MainPrefix = rows[ran.Next(0, arrLength)].MainPrefix,
            MainDescriptor = rows[ran.Next(0, arrLength)].MainDescriptor,
            MainDish = rows[ran.Next(0, arrLength)].MainDish,
            Connector = rows[ran.Next(0, arrLength)].Connector,
            SecondaryDescriptor = rows[ran.Next(0, arrLength)].SecondaryDescriptor,
            SecondaryDish = rows[ran.Next(0, arrLength)].SecondaryDish
        };

        return View(newCombo);
    }
    public ActionResult Create()
    {
        return View(new Combo());
    }

    [HttpPost]
    public ActionResult Create(Combo model)
    {
        db.Combos.Add(model);
        db.SaveChanges();
        return RedirectToAction("Create");
    }

    public ActionResult Edit(int id)
    {
        Combo editMe = db.Combos.Find(id);
        return View(editMe);
    }

    [HttpPost]
    public ActionResult Edit(Combo editMe)
    {
        if (ModelState.IsValid)
        {
            db.Entry(editMe).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        else
        {
            return View(editMe);
        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:51:42+00:00Added an answer on June 8, 2026 at 5:51 pm

    Another way to do it is to add an abstract property to the parent controller where the type is an interface implemented by the contexts you need to use.

    public RandomController : Controller
    {
        ...
        protected abstract IContext Db { get; }
        ...
    }
    
    public MenuController : RandomController
    {
        private SomeContext db = new SomeContext();
    
        ...
    
        protected override IContext Db { get { return db; } }
    
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing a Grails App. I have about 20 Controllers right now and there
Right now I have a script that will get the last five files in
Right now I have a function, in a class that is used to listen
Right now I have a link that causes a hidden div to appear when
Right now I have a foreach loop that grabs the first link with an
Right now I have a PHP file that does a MYSQL query and then
My application communicates with several GSM electric controllers, that means that I have to
I have a site that is moving incredibly slowly right now. Both Safari's inspector
Good day! I right now have a function the drags an element from a
Right now I have an upload field while uploads files to the server. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.