title: Update method for generic Entity framework repository
body: <p>I have a repository like that:</p>
  
  <pre><code>public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class 
  {
      private readonly IRepositoryContext _repositoryContext;
  
      public Repository(IRepositoryContext repositoryContext)
      {
          _repositoryContext = repositoryContext;
          _objectSet = repositoryContext.GetObjectSet&lt;T&gt;();
      }
  
      public virtual void Update(T entity)
      {
          ObjectSet.AddObject(entity);
          _repositoryContext.ObjectContext.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
          _repositoryContext.SaveChanges();
      }
    }
  </code></pre>
  
  <p>Now that actually works for all scalar properties of the entity, but all the other entities that associated with properties of entity <code>typeOf(T)</code>, don't care that entity state is modified, and EF simply adds new data. </p>
  
  <p>So, if you do for example <code>Repository&lt;Student&gt;.Update()</code>, and you only changed the name, it will find the right Student and change his name, but it also will change the Campus, although you already have a Campus associated with that student, it will be created again with a different CampusId.</p>
  
  <p>Show me please the correct way to do updates in this situation.</p>