title: Disposing of object context in entity framework 4
body: <p>I have an entity class which is auto generated from my database model. This class inherits the ObjectContext which inturn inherits IDisposable.</p>
  
  <p>I have created a repository that has various methods which use a single instance of the entity object to interact with the database.</p>
  
  <p>Auto generated class</p>
  
  <pre><code>public partial class DevEntities : ObjectContext
  {
      public const string ConnectionString = "name=DevEntities";
      public const string ContainerName = "DevEntities";
  </code></pre>
  
  <p>Repository Class</p>
  
  <pre><code>DevEntities db = new DevEntities();
  
          public Customer GetCustomerByID(int id)
      {
          var customers = db.Customers.FirstOrDefault(c =&gt; c.CustomerId == id);
  
          return customers;
      }
  
      public Customer GetCustomerByPasswordUsername(string email, string password)
      {
          var customers = db.Customers.FirstOrDefault(c =&gt; c.Email == email &amp;&amp; c.Password == password);
  
          return customers;
      }
  </code></pre>
  
  <p>From this you can see that I make multiple references to the db instance. My question is, am I better to instantiate a new DevEntity within each method, thus being able to implement the using statement, and so ensure correct disposal, or is my current implementation ok?</p>