2012-03-27 128 views
0

我有兩個表,通過外鍵CarrierID綁定:更新錯誤

Carrier[CarrierID,CarrierName] 
CarrierID = 1, CarrierName = DHL 
CarrierID = 2, CarrierName = Fedex 
... 
Vendor[VendorID, VendorName, CarrierID] 
VendorID = 1, VendorName =D-link , CarrierID=1 
VendorID = 2, VendorName = Netbes , CarrierID= 2 

當我嘗試更新vendorview的CARRIERNAME。即時得到這個錯誤「ForeignKeyReferenceAlreadyHasValueException」

我有課稱爲editvendor

public static void editvendor(Vendor vendor) 
     { 

      using (MyinvoiceDataDataContext connv = new MyinvoiceDataDataContext()) 
      { 

       Vendor editven = (from s in connv.Vendors 
           where s.VendorID == vendor.VendorID 
           select s).FirstOrDefault(); 

       editven.VendorAddress = editven.VendorAddress; 
       editven.VendorBalance = editven.VendorBalance; 
       editven.VendorContactName = editven.VendorContactName; 
       editven.VendorEmail = editven.VendorEmail; 
       editven.VendorFax = editven.VendorFax; 
       editven.VendorName = editven.VendorName; 
       editven.VendorPaymentTerms = editven.VendorPaymentTerms; 
       editven.VendorPhone = editven.VendorPhone; 
       editven.VendorRemark = editven.VendorRemark; 
       editven.VendorTax = editven.VendorTax; 
       editven.VendorWebsite = editven.VendorWebsite; 
       editven.CarrierID = editven.Carrier.CarrierID; 
       connv.SubmitChanges(); 

      } 
     } 
     #endregion 

在Windows窗體(編輯供應商的形式),我用這個:

private void Window_Loaded(object sender, RoutedEventArgs e) 
     { 

      MyinvoiceDataDataContext contecta = new MyinvoiceDataDataContext(); 
      var tb = from s in contecta.Carriers 
        select new { s.CarrierID, s.CarrierName }; 
      comVendorCarrier.ItemsSource = tb; 
      comVendorCarrier.DisplayMemberPath = "CarrierName"; 
      comVendorCarrier.SelectedValuePath = "CarrierID"; 

      ViewVendor vendview = new ViewVendor(); 
      txtVendorName.Text = vendor.VendorName.Trim(); 
      txtVendorBalance.Text = vendor.VendorBalance.Trim(); 
      txtVendorContactName.Text = vendor.VendorContactName.Trim(); 
      txtVendorPhone.Text = vendor.VendorPhone.Trim(); 
      txtVendorFax.Text = vendor.VendorTax.Trim(); 
      txtVendorEmail.Text = vendor.VendorEmail.Trim(); 
      txtVendorWebsite.Text = vendor.VendorWebsite.Trim(); 
      txtVendorRemarks.Text = vendor.VendorRemark.Trim(); 
      txtVendorAddress.Text = vendor.VendorAddress.Trim(); 
      txtVendorTax.Text = vendor.VendorTax.Trim(); 
      comVendorCarrier.Text = vendor.Carrier.CarrierName.Trim(); 
      ComVendorPaymentTerms.Text = vendor.VendorPaymentTerms.Trim(); 
     } 

此代碼我用它來保存編輯供應商

private void EditVendorSavebtn_Click_1(object sender, RoutedEventArgs e) 
     { 
      int SelectedCarrierId = int.Parse(comVendorCarrier.SelectedValue.ToString()); 
      if (txtVendorName.Text.Trim() == "") 
       MessageBox.Show("Please fill vendor's Name"); 

      if (txtVendorBalance.Text.Trim() == "") 
       MessageBox.Show("Please fill vendor's balance"); 

      if (txtVendorContactName.Text.Trim() == "") 
       MessageBox.Show("Please fill vendor's contact name"); 

      if (txtVendorPhone.Text.Trim() == "") 
       MessageBox.Show("Please fill vendor's phone number"); 

      if (txtVendorFax.Text.Trim() == "") 
       MessageBox.Show("Please fill vendor's fax number"); 

      if (txtVendorEmail.Text.Trim() == "") 
       MessageBox.Show("Please fill vendor's E-maill"); 

      if (txtVendorWebsite.Text.Trim() == "") 
       MessageBox.Show("Please fill vendor's Website"); 

      if (txtVendorAddress.Text.Trim() == "") 
       MessageBox.Show("Please fill vendor's address"); 

      else 
      { 

       vendor.VendorName = txtVendorName.Text.Trim(); 
       vendor.VendorBalance = txtVendorBalance.Text.Trim(); 
       vendor.VendorContactName = txtVendorContactName.Text.Trim(); 
       vendor.VendorPhone = txtVendorPhone.Text.Trim(); 
       vendor.VendorFax = txtVendorFax.Text.Trim(); 
       vendor.VendorEmail = txtVendorEmail.Text.Trim(); 
       vendor.VendorWebsite = txtVendorWebsite.Text.Trim(); 
       vendor.VendorRemark = txtVendorRemarks.Text.Trim(); 
       vendor.VendorAddress = txtVendorAddress.Text.Trim(); 
       vendor.VendorPaymentTerms = ComVendorPaymentTerms.Text; 
       vendor.CarrierID = SelectedCarrierId; 
       vendor.VendorTax = txtVendorFax.Text.Trim(); 
       vendorinfo.editvendor(vendor); 
       MessageBox.Show(vendor.VendorName + " succesfully edited."); 
       Window_Loaded(null, null); 
      } 
+2

我還沒有通讀所有的代碼,但你可以記住的是,使用Linq2SQL你通常不需要自己設置任何ID,只需添加一個對象實例的引用即可。因此,在你的情況下:'vendor.Carrier = carrier',其中'carrier'是'Carrier'的一個實例,同時也向'vendor'添加了正確的'CarrierID'。無需自己添加ID。 – Bazzz 2012-03-27 08:12:18

回答

1

我期望它是這種組合:

// somewhere 
comVendorCarrier.Text = vendor.Carrier.CarrierName.Trim(); 
... 
// somewhere else 
vendor.CarrierID = SelectedCarrierId; 

一種是使用面向對象的方法,一種是採用基於ID的方法 - 然而,如果這兩個被加載和不相容,問題。我想知道你是否應該使用:

vendor.Carrier = null; 
vendor.CarrierID = SelectedCarrierId; 

然後有一個使用哪個載體的定義。

可替代地,分開處理的載體,例如,代替:

comVendorCarrier.Text = vendor.Carrier.CarrierName.Trim(); 

使用:

var carrier = context.Carriers.Single(x => x.Id = vendor.CarrierId); 
comVendorCarrier.Text = carrier.CarrierName.Trim(); 

然後從未加載vendor.Carrier作爲對象。

+0

請檢查代碼,因爲'var carrier = context.Carriers.Single(x => x.Id = vendor.CarrierId); comVendorCarrier.Text = carrier.CarrierName.Trim();'不爲我工作。 – 2012-03-27 10:25:42

+0

@ThivakaranSelvarajoo定義「不工作」 - 會發生什麼? – 2012-03-27 13:04:09

+0

這裏有問題'var carrier = context.Carriers.Single(x => x.Id = vendor.CarrierId); comVendorCarrier.Text = carrier.CarrierName.Trim();' Id不能被發現。 – 2012-03-27 13:48:50