Note that there are some explanatory texts on larger screens.

plurals
  1. POClean method in order to update collection in entity framework
    primarykey
    data
    text
    <p>i'm sorry if this question has already been asked, but i'm in trouble with my method of updating collection in Entity Framework.</p> <p>Let me explain the situation : - I have for example one model CUSTOMER with some properties and a collection of ORDERS (for example). - Let's imagine we have an admin page on wich we can edit all the ORDERS for a customer, and when we submit the form, it will send us back the object CUSTOMERS with updated ORDERS (some added, some updated and some deleted).</p> <p>For the moment i use something like this in order to compare old collection and new collection and determine which object i need to delete/update/add</p> <pre><code> var toRemove = new List&lt;ORDERS&gt;(); var toAdd = new List&lt;ORDERS&gt;(); foreach ( var order in oldList.Where( order =&gt; newList.FirstOrDefault(t =&gt; t.link_id == order.link_id) == null)) { toRemove.Add(order); } foreach ( var order in newList.Where( order =&gt; oldList.FirstOrDefault(t =&gt; t.link_id == order.link_id) == null)) { toAdd.Add(order); } foreach (var ORDERSe in toRemove) { bdd.ORDERS.Remove(ORDERSe); } foreach (var ORDERSe in toAdd) { ORDERSe.pjt_id = project_id; bdd.ORDERS.Add(ORDERSe); } foreach ( var order in newList.Where( order =&gt; oldList.FirstOrDefault(t =&gt; t.link_id == order.link_id) != null)) { var child = oldList.FirstOrDefault(t =&gt; t.link_id == order.link_id); bdd.Entry(child).CurrentValues.SetValues(order); } </code></pre> <p>But i'm unconfortable with this, because in my mind, entity framework should be able to do the work for me !</p> <p>I was hoping something like : </p> <pre><code>customer.orders = newOrders; </code></pre> <p>Did i missed anything about entity framework or ? Because when i do this, it just duplicate my orders. Thanks in advance for your answer.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload