2016-08-12 55 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 
    } 
} 
相關問題