2015-06-29 59 views
0

我在Azure上有一個sql數據庫,並且可以像加載文章一樣加載數據。如果我嘗試在表格順序中存儲數據,billingDetails和position將在整個保存過程中運行,沒有任何異常,但如果我讓表格的數據在Visual Studio IDE中顯示後,則不會顯示新條目。實體框架6將外鍵數據插入到azure sql數據庫中

我的表順序,位置和billingDetails的流暢API看起來是這樣的:

//------Relationship Orders <--> Billing Details------- 
//Configure the primary key for orders (Primary key BillingDeatailID is foreign key in orders) 
modelBuilder.Entity<Orders>() 
.HasKey(b => b.BillingDetailID); 

//one-to-one relationship 
modelBuilder.Entity<BillingDetails>() 
.HasRequired(b => b.Order) 
.WithRequiredPrincipal(b => b.BillingDetails) 
.WillCascadeOnDelete(false); 

//------Relationship Products <--> Positions------- 
//one-to-many relationship (a position can have one product but a product can have many positions (optional relationship); the table positions contains ProductID as a required foreign key if the relation exists) 
modelBuilder.Entity<Positions>() 
.HasOptional<Products>(p => p.Product) 
.WithMany(p => p.Positions) 
.HasForeignKey(p => p.ProductID); 


//------Relationship Orders <--> Positions------- 
//one-to-many relationship (a position can have one order but an order can have many positions (optional relationship); the table positions contains OrderID as a required foreign key if the relation exists) 
modelBuilder.Entity<Positions>() 
.HasOptional<Orders>(o => o.Order) 
.WithMany(o => o.Positions) 
.HasForeignKey(o => o.OrderID); 

我保存數據操作:

public ActionResult CompleteOrder() 
     { 
      //save data to database 
      using (var context = new OnlineShopContext()) 
      { 
       BillingDetails billDetails = new BillingDetails(); 
       Orders order = new Orders(); 

       try 
       { 
        //save billing details 
        try 
        { 
         billDetails.Owner = Session["PaymentOwner"].ToString(); 
         billDetails.CardType = (int)Session["PaymentType"]; 
         if(Session["PaymentType"].ToString() == "0"){ 
          billDetails.Number = Session["PaymentCreditcardNumber"].ToString(); 
         }else{ 
          billDetails.Number = Session["PaymentAccountNumber"].ToString(); 
         } 

         billDetails.ExpiryMonth = (int)Session["PaymentExpireMonth"]; 
         billDetails.ExpiryYear = (int)Session["PaymentExpireYear"]; 
         billDetails.Swift = Session["PaymentSwift"].ToString(); 
         billDetails.Blz = Session["PaymentBlz"].ToString(); 
         billDetails.IBAN = Session["PaymentIBAN"].ToString(); 

         context.BillingDetails.AddOrUpdate(billDetails); 
         context.Entry(billDetails).State = EntityState.Added; 

         if (context.SaveChanges() > 0) 
         { 
          //saved 
         } 
         else 
         { 
          string Msg = "Error while saving!"; 

          return View((object)Msg); 
         } 
        } 
        catch (OptimisticConcurrencyException ocEx) 
        { 
         log.Fatal("OptimisticConcurrencyException while saving billing details: " + ocEx.Message); 
         string Msg = "Error while saving!"; 

         return View((object)Msg); 
        } 

        //get the id of added billing details item and complete the order 
        var billingDetailsId = billDetails.BillingDetailID; 

        order.BillingDetailID = billingDetailsId; 

        order.DeliveryName = Session["DeliveryName"].ToString(); 
        order.DeliveryStreet = Session["DeliveryStreet"].ToString(); 
        order.DeliveryCity = Session["DeliveryCity"].ToString(); 
        order.DeliveryZipCode = Session["DeliveryZipCode"].ToString(); 
        order.DeliveryCountry = Session["DeliveryCountry"].ToString(); 

        order.BillName = Session["BillName"].ToString(); 
        order.BillStreet = Session["BillStreet"].ToString(); 
        order.BillCity = Session["BillCity"].ToString(); 
        order.BillZipCode = Session["BillZipCode"].ToString(); 
        order.BillCountry = Session["BillCountry"].ToString(); 

        order.OrderDate = DateTime.Now; 

        //save the order 
        try 
        { 
         context.Orders.AddOrUpdate(order); 
         context.Entry(order).State = EntityState.Added; 
         if(context.SaveChanges() > 0){ 
          //saved 
         } 
         else{ 
          string Msg = "Error while saving!"; 

          return View((object)Msg); 
         } 
        } 
        catch (OptimisticConcurrencyException ocEx) 
        { 
         log.Fatal("OptimisticConcurrencyException while saving order: " + ocEx.Message); 
         string Msg = "Error while saving!"; 

         return View((object)Msg); 
        } 

        //get id of added order 
        var orderId = order.OrderID; 

        //save all positions of this order 
        foreach (var item in CartItems) 
        { 
         Positions position = new Positions(); 
         position.OrderID = orderId; 
         position.ProductID = item.product.ProductID; 
         position.Amount = item.amount; 

         try 
         { 
          context.Positions.AddOrUpdate(position); 
          context.Entry(position).State = EntityState.Added; 

          if(context.SaveChanges() > 0){ 
           //saved 
          } 
          else{ 
           string Msg = "Error while saving!"; 

           return View((object)Msg); 
          } 
         } 
         catch (OptimisticConcurrencyException ocEx) 
         { 
          log.Fatal("OptimisticConcurrencyException while saving position: " + ocEx.Message); 
          string Msg = "Error while saving!"; 

          return View((object)Msg); 
         } 
        } 

       } 
       catch (Exception ex) 
       { 
        log.Fatal("Error while saving order data. Exception: " + ex.Message); 

        string Msg = "Error while saving!"; 

        return View((object)Msg); 
       } 

       //empty the shopping cart 
       RemoveAllCartItems(); 

       //redirect to the catalog 
       return RedirectToAction("Index", "Products"); 
      } 
     } 

的ID是正確遞增當我檢查他們,而調試(例如ID 9),並且如果我再次重現它,則ID再次遞增(例如ID 10)。

數據庫中已經有一些虛擬數據,它們不會改變,所以它也不會意外更新它們。

爲什麼我新添加的數據不顯示,如果我嘗試在IDE中顯示它們?

+0

也許你在VS IDE中找錯了位置? – ErikEJ

+0

我不認爲這是錯誤的地方,因爲我試圖在我創建的數據庫連接中的服務器瀏覽器中看到它。但是你也是對的,因爲有一個更好的地方可以讀取數據。直接在服務器資源管理器中鏈接的azure - > sql數據庫區域中。 謝謝你的提示。感謝你我再次檢查。 :) – Christian

回答

0

您需要在這種情況下檢查的第一件事是您正在從數據讀取數據並將數據寫入同一數據庫。

,我在你的代碼注意到的另一件事是:

 context.BillingDetails.AddOrUpdate(billDetails); 
     context.Entry(billDetails).State = EntityState.Added; 

爲什麼你需要設置實體狀態?

+0

我找到了一個解決方案。我創建了一個新視圖來讀取並顯示我插入數據的表的內容,並且所有數據都可用。 但我認爲這很奇怪。我在服務器資源管理器中創建的數據庫連接不顯示新數據,並且服務器資源管理器中鏈接的azure中的sql數據庫區域顯示所有數據。 無論如何,我刪除了我設置實體狀態的3行。我以爲我需要這條線來告訴實體框架有所改變。那麼我應該什麼時候設置這個狀態? – Christian