現在、私は3つのコントローラーを持っています。Random (親)、Menu & Name (子)。
データベースで動作する RandomController のメソッドがいくつかありますが、Menu と Name で異なるデータベースを指定する必要があります。ただし、Random からデータベース コンテキスト宣言を削除すると、あらゆる種類のエラーがスローされます。
補足として、Random は単独でアクセスされることはありません。メニューと名前のコードを提供するためにのみ存在します。
コントローラー全体が実際に必要なわけではありませんが、アイデアを提供する必要があるいくつかの方法を次に示します。すべてのデータベース。ComboContext 宣言を子に移動すると、ステートメントが壊れます。
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);
}
}
}