title: Entity Framework DbContext getting disposed
body: <p>I have a little program that uses <a href="http://objectlistview.sourceforge.net/cs/index.html" rel="nofollow">ObjectListView</a> to visualise the database information. Currently I am trying to add items and keep getting <code>The operation cannot be completed because the DbContext has been disposed.</code> Which is highlighting the <code>olvEntityTypes.AddObject(et);</code> line below.</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>string typeName = tbTypeName.Text;
  string desc = tbDesc.Text;
  
  EntityTypes et;
  using (FCERTSModelContainer db = new FCERTSModelContainer())
  {
      // Create and save new row for the EntityTypes table.
      et = new EntityTypes { EntityTypeName = typeName, Description = desc };
      db.EntityTypes.Add(et);
      db.SaveChanges();
  
      olvEntityTypes.AddObject(et);
  }
  </code></pre>
  
  <ul>
  <li>I suspected that <code>SaveChanges()</code> might also be closing the context, but trying to add the object before saving changes still throws the same error.  </li>
  <li>Putting <code>olvEntityTypes.AddObject(et);</code> outside of the <code>using</code> block is also a failure.</li>
  <li>If I query the database with something like the following, no errors are thrown;</li>
  </ul>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>var query = from ent in db.EntityTypes
              orderby ent.EntityTypeName
              select ent;
  
  olvEntityTypes.SetObjects(query);
  </code></pre>
  
  <p>So I'm at a bit of a loss now as to why this is throwing errors and how to fix it.</p>