LINQ: relationships are not updated after SubmitChanges and Refresh

I found something strange. Let's say I have a Car model and a Company model.

So, my Car model has a CompanyId string.

Say I changed the CompanyId Car , then I SubmitChanges in the DataContext .

Here is my problem: Updated CompanyId of my Car model. But the attitude of Car.Company is NOT.

So, if I change Car.CompanyId from 1 to 2. Then, outputting Car.Company.CompanyId , it will display 1 instead of 2.

So my question is: is there a way to update EVERYTHING in a DataContext?

I am currently using:

public void RefreshCollection() { // On rafraichit les données à partir de la DB. dataContext.Refresh(RefreshMode.KeepChanges); // On va chercher les données complête de la table. purchaseOrders = from po in dataContext.PurchaseOrders orderby po.PurchaseOrderId ascending select po; } 

Refresh my collection.

and

 private void sauvegarderToolStripMenuItem_Click(object sender, EventArgs e) { purchaseOrder.OrderDate = orderDate.Value; purchaseOrder.RequiredDate = requiredDate.Value; purchaseOrder.ShipTo = shipTo.Text; purchaseOrder.State = helper.ConvertComboBoxIndexToStateIndex( stateKey.SelectedIndex); // On cast un autre type pour le forcer à être un ComboBoxItem var supplierItem = (ComboBoxItem)supplierId.SelectedItem; purchaseOrder.SupplierId = supplierItem.Id; dataContext.SubmitChanges(); } 

Submit your changes.

+4
source share
5 answers

You can check the answers to this question .

+1
source

I think your line

  purchaseOrder.SupplierId = supplierItem.Id; 

should read more

  purchaseOrder.Supplier = supplierItem; 

Just guess the properties.

You can also call dataContext.Refresh () on PurchaseOrder after SaveChanges ()

+2
source

Simply changing the identifier is not enough to also change the associated company object. You need to upload the Company object that you want and assign it to Car.Company. This will also update the Car.CompanyID property.

Sort of:

 var car = context.Cars.First(); // pick your car var newCompany = context.Companies.First(); // pick your company car.Company = newCompany; context.SubmitChanges(); 
+1
source

Not sure if this is a viable option here, but you always have the opportunity to take your request, including its related objects, and attach it like this:

 purchaseOrders = from po in dataContext.PurchaseOrders.Include("Company") orderby po.PurchaseOrderId ascending select po; dataContext.Attach(purchaseOrders); 
+1
source

Try to open a separate DataContext for each operation. Ideally, any update operation should end with SubmitChanges and close the DataContext. A DataContext is a lightweight object and should not contain too much overhead in this scenario. For instance:

 public void RefreshCollection() { using (var dataContext = CreateDataContext()) { // On va chercher les données complête de la table. purchaseOrders = from po in dataContext.PurchaseOrders orderby po.PurchaseOrderId ascending select po; } } private void sauvegarderToolStripMenuItem_Click(object sender, EventArgs e) { using (var dataContext = CreateDataContext()) { purchaseOrder.OrderDate = orderDate.Value; purchaseOrder.RequiredDate = requiredDate.Value; purchaseOrder.ShipTo = shipTo.Text; purchaseOrder.State = helper.ConvertComboBoxIndexToStateIndex( stateKey.SelectedIndex); // On cast un autre type pour le forcer à être un ComboBoxItem var supplierItem = (ComboBoxItem)supplierId.SelectedItem; purchaseOrder.SupplierId = supplierItem.Id; dataContext.SubmitChanges(); } } 
0
source

Source: https://habr.com/ru/post/1337349/


All Articles