3
我在asp.net mvc 3中有一个小项目,我使用RavenDB来存储数据。 但是,当我试图更新实体我有错误说 “试图将不同的对象ID为关联的订单/ 257'” 我有一个服务类家政实体。RavenDB与asp.net mvc更新问题
这是更新名为Order的实体的方法。 心中已经省略清晰
public ErrorState UpdateOrder(Order order)
{
try
{
documentSession.Store(order);
documentSession.SaveChanges();
return new ErrorState { Success = true };
}
catch (Exception ex)
{
return new ErrorState { Success = false, ExceptionMessage = ex.Message };
}
}
的方法baceuse其余这是OrderRepository
private readonly IDocumentSession documentSession;
public OrderRepository(IDocumentSession _documentSession)
{
documentSession = _documentSession;
}
ErrorState类是menagege错误的应用休息,它包含布尔成功和异常的字符串信息。
这是我的编辑操作。
public ActionResult Edit(int id)
{
Order order = orderRepository.ObtainOrder(id);
if (order == null)
{
TempData["message"] = string.Format("Order no: {0} not found", id);
return RedirectToAction("Index");
}
return View(order);
}
[HttpPost]
public ActionResult Edit(Order order)
{
if(!ModelState.IsValid)
return View();
errorState = orderRepository.UpdateOrder(order);
if (errorState.Success)
{
TempData["message"] = string.Format("Order no: {0} has been changed", order.Id);
return RedirectToAction("Index");
}
else
{
TempData["Message"] = string.Format("Error on update order no: {0} MSG: {1}", order.Id,errorState.ExceptionMessage);
return RedirectToAction("Index");
}
}
这是控制器的其余部分,我忽略了清晰度的其他操作。
private readonly IOrderRepository orderRepository;
private ErrorState errorState;
public HomeController(IOrderRepository _orderRepository,IDocumentSession _documentSession)
{
orderRepository = _orderRepository;
}
感谢您的回复。 – Konrad
是的,我有跨请求相同的会话,这意味着如果我重新调整应用程序数据库包含我输入的所有条目。 – Konrad
我的会话在请求范围内 – Konrad