new features c# wpf linq-to-sql data-binding datacontext

c# 7 new features



Error de actualizaciĆ³n usando(ling a sql) (1)

Espero que sea esta combinación:

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

Uno está utilizando el enfoque orientado a objetos, uno está utilizando el enfoque basado en id. Sin embargo, si ambos están cargados y son incompatibles , hay problemas. Me pregunto si deberías usar:

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

entonces hay exactamente una definición de qué operador usar.

Alternativamente, maneje el operador por separado, por ejemplo, en lugar de:

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

utilizar:

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

que luego nunca carga el vendor.Carrier . vendor.Carrier como un objeto.

Tengo dos tablas, vinculadas por clave externa 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

Cuando intento actualizar el nombre del operador en la vista de proveedor. Recibo este error "ForeignKeyReferenceAlreadyHasValueException"

Tengo clase llamada 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

En el formulario de Windows (editar el formulario del proveedor), uso esto:

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(); }

Este código lo uso para guardar el proveedor editado

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); }