使用(linq to sql)更新错误

时间:2012-03-27 08:06:58

标签: c# wpf linq-to-sql data-binding datacontext

我有两个表,由外键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中的运营商名称时。我收到此错误“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);
            }

1 个答案:

答案 0 :(得分: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作为对象。