title: Refresh DataContext when using Repository Pattern returning IQueryable data
body: <p>I have an isolated problem with cached data in a web app, and understand that it's cached due to the way the datacontext is reused.  My Repository classes return IQueryable types, so I'm unable to refresh the datacontext by closing &amp; reopening it.  For example, my CustomerRepository has this datacontext constructor:</p>
  
  <pre><code>private CAClassesDataContext context = new CAClassesDataContext();
  </code></pre>
  
  <p>And all database interaction methods then use this context, e.g:</p>
  
  <pre><code>public IQueryable&lt;Customer&gt; Customers
  {
       get { return context.Customers; }
  }
  </code></pre>
  
  <p>If I try to use the context in a using block:</p>
  
  <pre><code>using(CAClassesDataContext context = new CAClassesDataContext())
  {
  ...
  }
  </code></pre>
  
  <p>I can't access the related classes as lazy loading is used.  I've tried adding:</p>
  
  <pre><code>context.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, customer)
  </code></pre>
  
  <p>after the update but the problem still exists.  How can I force LINQ to SQL to use the database data instead of the cached?</p>
  
  <p><strong>UPDATE</strong>:
  Thanks to answers provided by Steve &amp; Andrew I was able to resolve the issue.  My respositories now have a constructor like this:</p>
  
  <pre><code>[Inject]
  public CustomerRepository (CAClassesDataContext Context)
  {
      context = Context;
  }
  </code></pre>
  
  <p>And in my Ninject bindings added:</p>
  
  <pre><code>Bind&lt;CAClassesDataContext&gt;().ToSelf().InRequestScope();
  </code></pre>
  
  <p>My project uses a custom membership provider an I was unable to use the above technique due to the provider's parameterless constructor. To get round this I added methods to the respository which get called before &amp; after the databse lookup:</p>
  
  <pre><code>repository.CreateContext();
  var cust = repository.GetAllCustomers().SingleOrDefault(c =&gt; c.Username == username);
  repository.DisposeContext();
  </code></pre>