title: Managing Entity Framework ObjectContext in ASP.NET
body: <p>I'm using the Entity Framework for an ASP.NET Web Forms application and I'm wondering how I should deal with <code>ObjectContext</code> and it's lifetime.
  For example, I have an <code>InviteService</code> class that manages invites such as creating and accepting invites. The class itself is in another project/namespace from the Web project.
  An <code>InviteUsers()</code> method creates <code>Invite</code> entities for a list of users, calls a repository to save them to the database and mails each user an invite link.</p>
  
  <p>The method is called from the <code>Page</code> when a user clicks the invite button.</p>
  
  <p>I would like to know how I should use the <code>ObjectContext</code></p>
  
  <ol>
  <li>Instantiate a new <code>ObjectContext</code> on the Page on each Request, passing it as a parameter to the constructor of the <code>InviteService</code> class and then disposing it in the <code>Render</code> method.</li>
  <li>Same as above but instead of setting it via the constructor, passing it along as a parameter to each method.</li>
  <li>Create a separate <code>Objectcontext</code> in each method with a <code>using</code> block.</li>
  </ol>
  
  <p>Option one seems best to me based on the answer of Ladislav here: <a href="https://stackoverflow.com/questions/3653009/entity-framework-and-connection-pooling/3653392#3653392">Entity Framework and Connection Pooling</a>
  But option 3 seems valid as well since as far as I know, no new database connections are made because of connection pooling.</p>