实体框架模型未正确更新

时间:2016-02-13 19:54:56

标签: c# asp.net-mvc entity-framework

我的控制器中有一个方法,用于检查是否已经过了一定的时间,如果是,则更新模型属性并将其保存到数据库中。然而,该模型没有更新,我不确定为什么,如果有人可以提供任何建议?谢谢!目前正在从数据库中删除订单,但不会更改eventDetails.TicketsRemaining。

 [HttpPost]
 [ValidateAntiForgeryToken]
 public ActionResult Charge(SummaryVm summaryVm)
 {

 if (checkDateTime(summaryVm.orderDate))
  {

  //do something

 }else{

  // get the event being booked from db based on the ID
  Event eventDetails = _eventRepository.GetByID(summaryVm.eventID);

  // get the order being processed from db based on the ID
  Order thisOrder = db.Orders.Find(summaryVm.orderID);

 //update the number of tickets remaining for event returning the number of tickets requested to the available tickets
  eventDetails.TicketsRemaining += summaryVm.ticketQuantity;

 //remove order from db as payment was not received and order was not processed
 db.Orders.Remove(thisOrder);

 //save these db changes
 db.SaveChanges();

 return RedirectToAction("TimeOut", new { id = summaryVm.eventID });

  }
}

1 个答案:

答案 0 :(得分:0)

_eventRepository.SaveChanges()

当您混合设计模式时,您的代码很奇怪......

如果您创建了_eventRepository

,则

db不应该存在

您已经创建了两个上下文..并且您没有将保存更改提交给您更改数据的那个。

ALTERNATIVELY

你可以通过

来解决
  Event eventDetails = db.Events.GetByID(summaryVm.eventID);