问题
I have two tables, binded by foreign key 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
When i try to update the carriername in vendorview. Im getting this error "ForeignKeyReferenceAlreadyHasValueException"
I have class called 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
In the windows form(edit vendor form) i use this:
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();
}
This code i use to save the edited vendor
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:
I expect it is this combination:
// somewhere
comVendorCarrier.Text = vendor.Carrier.CarrierName.Trim();
...
// somewhere else
vendor.CarrierID = SelectedCarrierId;
One is using the object-oriented approach, one is using the id-based approach - however, if both are loaded and are incompatible, problems. I wonder if you should use:
vendor.Carrier = null;
vendor.CarrierID = SelectedCarrierId;
then there is exactly one definition of which carrier to use.
Alternatively, handle the carrier separately, for example, instead of:
comVendorCarrier.Text = vendor.Carrier.CarrierName.Trim();
use:
var carrier = context.Carriers.Single(x => x.Id = vendor.CarrierId);
comVendorCarrier.Text = carrier.CarrierName.Trim();
which then never loads vendor.Carrier
as an object.
来源:https://stackoverflow.com/questions/9885568/updating-error-using-ling-to-sql