title: Generic Insert or update method
body: <p>I have a generic method that updates or inserts and entity. I don't understand the purpose of the if statement here. The comment tells me what it is for but it is not clear to me. </p>
  
  <p>Why is the namespace checked against System.Data.Entity.DynamicProxies. I thought that all entities retrieved via entity framework would be under this namespace? And why would we check the base type for null? </p>
  
  <pre><code>public virtual T InsertOrUpdate(T e)
  {
  DbSet&lt;T&gt; dbSet = Context.Set&lt;T&gt;();
  
  DbEntityEntry&lt;T&gt; entry;
  if (e.GetType().BaseType != null 
      &amp;&amp; e.GetType().Namespace == "System.Data.Entity.DynamicProxies")
  {
      //The entity being added is already a proxy type that supports lazy 
      //loading - just get the context entry
      entry = Context.Entry(e);
  }
  else
  {
      //The entity being added has been created using the "new" operator. 
      //Generate a proxy type to support lazy loading  and attach it
      T instance = dbSet.Create();
      instance.ID = e.ID;
      entry = Context.Entry(instance);
      dbSet.Attach(instance);
  
      //and set it's values to those of the entity
      entry.CurrentValues.SetValues(e);
      e = instance;
  }
  
  entry.State = e.ID == default(int) ?
                          EntityState.Added :
                          EntityState.Modified;
  
      return e;
  }
  </code></pre>