title: Repository Pattern in Entity framework 4 when should we dispose?
body: <p>New to EF and I have noticed that using a repository pattern can really simplify things and will allow me to do some mocking too.So far so good.</p>
  
  <p>My Question</p>
  
  <p>A typical usage of the objectContext is to destroy as soon as possible see below</p>
  
  <pre class="lang-cs prettyprint-override"><code>using (var context = new SchoolEntities())
  {    
      context.AddToDepartments(department);    
      context.SaveChanges();
  }
  </code></pre>
  
  <p>Using the Repository pattern I have noticed that nobody actually uses the "Using Pattern" eg</p>
  
  <pre class="lang-cs prettyprint-override"><code>using (var repository= new Repository&lt;Student&gt;(new MyContext))
  {    
      repository.Add(myStudentEntity)  
      repository.SaveChanges();
  }
  </code></pre>
  
  <p>Should the idea be that we should dispose of the context as soon as possible otherwise the memory might leak or get very big?</p>
  
  <p>Can anyone clarify? Thanks a lot.</p>