1

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);
        }

    }
}

2 Answers 2

1

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; } }

    ...
}
1

Leave the db context in the parent controller as a protected member, and then pass the appropriate context into the constructor of each child controller using DI.

public class RandomController : Controller   
{
    protected readonly CombosContext db;

    protected RandomController (CombosContext db)
    {
        this.db = db;
    }

    ...
}


public class MenuController : RandomController
{
    public MenuController (CombosContext db)
        : base (db)
    {
    }
}

Or, if you don't want to use dependency injection, you can just initialise the context inside the child constructors.

3
  • Or as a constructor parameter or abstract property.
    – SLaks
    Jul 30, 2012 at 0:55
  • This works fine for the Menu controller using Combos Context (like the parent), but when I get to the Name controller and begin using the NamesContext in the constructor, I get: Error 1 The best overloaded method match for 'Controllers.RandomController.RandomController(Models.CombosContext)' has some invalid arguments Error 2 Argument 1: cannot convert from 'Models.NameContext' to 'Models.CombosContext'
    – Nick Brown
    Jul 30, 2012 at 1:12
  • 1
    You need to use an interface common to both contexts for the type of the member. Jul 30, 2012 at 1:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.