title: Generic Insert or Update for Entity Framework
body: <p>Say I have an insert method:</p>
  
  <pre><code>public T Add&lt;T&gt;(T t)
  {
     context.Set&lt;T&gt;().Add(t);
     context.SaveChanges();
     return t;
  }
  </code></pre>
  
  <p>And a generic update:</p>
  
  <pre><code>public T Update&lt;T&gt;(T updated,int key)
  {
      if (updated == null)
          return null;
  
      T existing = _context.Set&lt;T&gt;().Find(key);
  
      if (existing != null)
      {
          context.Entry(existing).CurrentValues.SetValues(updated);
          context.SaveChanges();
      }
  
      return existing;
  }
  </code></pre>
  
  <p>I'd like to combine them into one <code>SaveOrUpdate</code> that accepts any entity method:</p>
  
  <p>How would I best achieve this and is there a more efficient way that avoids a round trip to the database than using <code>context.Set&lt;T&gt;().Find(key)</code></p>