このトピックについては、多くの質問がありますが、私のシナリオに関連するものは何も見つかりません。DBContextがコントローラで生成されていますが、2つのオブジェクト間の関係...異なるObjectContextオブジェクト
Imはこれがすべて失敗私はそれを実行した非常に最初の時間とそれに続く試みを働いたInventory CurrentInventory = db.Inventories.Find(injection.InventoryID);
に「彼らは異なるのObjectContextのオブジェクトに添付されているので、2つのオブジェクト間の関係を定義することはできません」取得。
私は別のコンテキストオブジェクトをどのように取得しているのか分かりません。
マイコントローラー:
public class InventoriesController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Inventories/Edit
public ActionResult Edit()
{
return View(db.Inventories.ToList());
}
// POST: Inventories/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(List<Inventory> inventory)
{
//List<Inventory> inventory
if (inventory == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
foreach (Inventory injection in inventory)
{
Inventory CurrentInventory = db.Inventories.Find(injection.InventoryID);
CurrentInventory.Date = DateTime.Now;
CurrentInventory.Quantity = injection.Quantity;
CurrentInventory.LastChangedBy = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
}
if (ModelState.IsValid)
{
// db.Entry(inventory).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(EntityState.Modified);
}
}
編集:私はそれが役立つかどうちょうど見に戻ってより多くの単一の更新方法論へ行ってきました
。私はまだ同じエラーが発生していました。だから私は他のコントローラの編集メソッドを修正したものと比較して、代わりに.Find()メソッドを使わなかったことに気付きました。自分自身でレコードを繰り返し、目的のオブジェクトを返しました。やっているときに問題は解決しました。
// GET: Inventories/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Inventory inventory = db.Inventories.Find(id);
ViewData["Id"] = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
if (inventory == null)
{
return HttpNotFound();
}
return View(inventory);
}
// POST: Inventories/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "InventoryID,Quantity,LastChanged,Date")] Inventory inventory)
{
Inventory oldInventory = null;
foreach (var a in db.Inventories)
{
if (a.InventoryID == inventory.InventoryID)
{
oldInventory = a;
}
}
if (oldInventory == null)
{
return HttpNotFound();
}
if (ModelState.IsValid)
{
//db.Entry(inventory).State = EntityState.Modified;
TryUpdateModel(oldInventory);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(inventory);
}
フォローアップの質問:返されるオブジェクトは全く異なる文脈から本質的になるように
.Find()メソッドは、その実装に別のdbContextを作成していますか?
インベントリCurrentInventorydb.Inventories.Find(x => x.InventoryID == inject.InventoryID); –