title: Entity framework 6 : generic method AddOrUpdate
body: <p>I spend so much time trying implement a generic method to add or update a entity with a related entity (relation one to many) but I am stuck...</p>
  
  <p>The method must receives 2 parameters, the first is the parent, second is the child. The goal is to save child entity into parent (adding if does not exist or update)</p>
  
  <p>There is the generic method signature:</p>
  
  <pre><code>    public static bool AddOrUpdate&lt;T,U&gt;(T ItemToSave,U ItemRelated, int ID) where T : class where U : class
      {
          using (var context = new dbContext())
          {                
              var parent = context.Set&lt;T&gt;().Find(ID);
              if (parent == null) return;
              // how to retrieve now the child (ItemRelated) from the parent (ItemToSave) in order to add into it or update ?
  
              return context.SaveChanges() &gt; 0;
          }
      }
  </code></pre>
  
  <p>This method is located in a static class 'Service'
  and I want to be able to call Service.AddOrUpdate(Order _order, OrderLine _orderline, _order.OrderId) from any class.</p>
  
  <p>I am stuck at retrieving the child from parent and to add or update into it.</p>
  
  <p>Can anyone please help me to achieve that ?</p>