2016-08-12 57 views
0

我是一个网页编程的业余爱好者。 目前正在使用c#,MVC,js/ts和jquery。'System.Data.Entity.Infrastructure.DbUpdateException'我不知道为什么我的程序将这个错误扔给我

当我尝试调用SaveChanges我的数据库中,我得到这个错误:

" 'System.Data.Entity.Infrastructure.DbUpdateException' occurred in EntityFramework.dll. Additional information: An error occurred while updating the entries. See the inner exception for details. "

有没有内部细节。这是我想要做的。

 Order order = new Order(); 
     TryUpdateModel(order); 

     try 
     { 
      if (string.Equals(values["PromoCode"], PromoCode, 
       StringComparison.OrdinalIgnoreCase) == false) 
      { 
       return View(order); 
      } 
      else 
      { 
       order.Username = User.Identity.Name; 
       order.OrderDate = DateTime.Now; 

       //Save Order 
       storeDB.Orders.Add(order); 
       storeDB.SaveChanges(); 
       //Process the order 
       var cart = ShoppingCart.GetCart(this.HttpContext); 
       cart.CreateOrder(order); 

       return RedirectToAction("Complete", 
        new { id = order.OrderId }); 
      } 
     } 
     catch (System.Data.Entity.Core.UpdateException e) 
     { 
      return View(order); 
     } 

     catch (System.Data.Entity.Infrastructure.DbUpdateException ex) //DbContext 
     { 
      Console.WriteLine(ex.InnerException); 
      return View(order); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.InnerException); 
      //Invalid - redisplay with errors 
      return View(order); 
     } 

失败在cart.CreateOrder(order);

这是CreateOrder(订单)确实

 decimal orderTotal = 0; 

     var cartItems = GetCartItems(); 
     // Iterate over the items in the cart, 
     // adding the order details for each 
     foreach (var item in cartItems) 
     { 
      var orderDetail = new OrderDetail 
      { 
       GameId = item.GameId, 
       OrderId = order.OrderId, 
       UnitPrice = item.Game.Price, 
       Quantity = item.Count 
      }; 
      // Set the order total of the shopping cart 
      orderTotal += (item.Count * item.Game.Price); 

      storeDB.OrderDetails.Add(orderDetail); 

     } 
     // Set the order's total to the orderTotal count 
     order.Total = orderTotal; 

     // Save the order 
     storeDB.SaveChanges(); 
     // Empty the shopping cart 
     EmptyCart(); 
     // Return the OrderId as the confirmation number 
     return order.OrderId; 

它给了我在storeDB.SaveChanges错误消息();所有的东西都拼写成它所假设的方式。

你们认为我错过了什么?

+0

能否请您检查'OrderId'得到填充在'order'对象保存'order'后首次进行调用'storeDB.SaveChanges()之前;'?它是否具有一些在数据库中反映的有效值? – RBT

+1

@RBT是的,它在我的数据库中有一个有效的值。当我说问题出现在这里时,我想你明白了。order.Total = orderTotal //保存订单 storeDB.SaveChanges();(with saveChanges)not here->'storeDB.Orders.Add(order); storeDB.SaveChanges();' –

+0

@RBT一切顺序都很好,它的orderDetails是有问题的 –

回答

0

我们计算出来了。当我们将更改保存到数据库时,我们忘记填写该条目的列。非常愚蠢= P。我正在和一个合作伙伴一起工作,并认为他们会照顾所有这些东西。

谢谢你们

0

DbUpdateException是由大多数数据库约束违规引起的。您应该显示处理DbUpdateException的附加代码:

try 
{ 
    .... 
} 
catch (DbUpdateException ex) 
{ 
    UpdateException updateException = (UpdateException)ex.InnerException; 
    SqlException sqlException = (SqlException)updateException.InnerException; 

    foreach (SqlError error in sqlException.Errors) 
    { 
     // TODO: Do something with your errors 
    } 
} 
相关问题