2017-03-04 86 views
0

我知道这个主题有很多问题,但我似乎找不到与我的场景有关的任何问题。DBContext在控制器中生成,仍然得到两个对象之间的关系...不同的ObjectContext对象

即时得到“两个对象之间的关系不能被定义,因为它们连接到不同的ObjectContext对象”上Inventory CurrentInventory = db.Inventories.Find(injection.InventoryID);

这工作很我第一次运行它,并随后尝试都失败。

我不明白我是如何获得不同的上下文对象。

我的控制器:

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

编辑:

我回到了一个较为单一更新方法,只是为了看看是否会有帮助。我仍然得到相同的错误。所以我将其与其他控制器之一进行了比较,修改了Edit方法,并注意到我没有使用.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使返回的对象本身是从完全不同的环境?

回答

0

对控制器中的私有上下文进行迭代并返回所需的对象而不是使用.Find()方法解决了我的问题,尽管我并不太热衷于此原因。

+0

你应该做库存CurrentInventorydb.Inventories.Find(x => x.InventoryID == injection.InventoryID); –

相关问题