title: System.ObjectDisposedException: 'The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.'2022
body: <p>Im doing a MVC app but when I try to run it, it shows the following errors</p>
  <p><strong>System.ObjectDisposedException: 'The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.'
  item.Plan.Plan_Days = 'item.Plan.Plan_Days' threw an exception of type 'System.NullReferenceException'</strong></p>
  <pre><code>@model IEnumerable&lt;POS.Models.Faculty&gt;
  
  @{
      ViewBag.Title = &quot;Index&quot;;
  }
  
  &lt;h2&gt;Index&lt;/h2&gt;
  
  &lt;p&gt;
      @Html.ActionLink(&quot;Crear Nuevo&quot;, &quot;Guardar&quot;)
  &lt;/p&gt;
  &lt;table class=&quot;table&quot;&gt;
      &lt;tr&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Organization_Name)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.First_Name)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Last_Name)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Job_Title)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Email)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Photo)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Address)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Phone_Number)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Meal_Plan_Status)
          &lt;/th&gt;
          &lt;th&gt;
              @Html.DisplayNameFor(model =&gt; model.Plan.Plan_Days)
          &lt;/th&gt;
          &lt;th&gt;&lt;/th&gt;
      &lt;/tr&gt;
  
  @foreach (var item in Model) {
      &lt;tr&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Organization_Name)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.First_Name)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Last_Name)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Job_Title)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Email)
          &lt;/td&gt;
          &lt;td&gt;
              &lt;img src=&quot; @Html.DisplayFor(modelItem =&gt; item.Photo)&quot; width=&quot;200&quot; /&gt;
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Address)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Phone_Number)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Meal_Plan_Status)
          &lt;/td&gt;
          &lt;&lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.Plan.Plan_Days)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.ActionLink(&quot;Edit&quot;, &quot;Edit&quot;, new { id=item.Employee_ID }) |
              @Html.ActionLink(&quot;Details&quot;, &quot;Details&quot;, new { id=item.Employee_ID }) |
              @Html.ActionLink(&quot;Delete&quot;, &quot;Delete&quot;, new { id=item.Employee_ID })
          &lt;/td&gt;
      &lt;/tr&gt;
  }
  
  &lt;/table&gt;
  </code></pre>
  <p>This is the controller:</p>
  <pre class="lang-cs prettyprint-override"><code>using POS.Datos;
  using POS.Models;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  
  namespace POS.Controllers
  {
      public class FacultyController : Controller
      {
          FacultyAdmin admin = new FacultyAdmin();
  
          // GET: Faculty
          public ActionResult Index()
          {
              return View(admin.Consultar());
          }
  
          public ActionResult Guardar()
          {
              ViewBag.mensaje = &quot;&quot;;
              return View(); 
          }
  
          public ActionResult Nuevo(Faculty modelo)
          {
              admin.Guardar(modelo);
              ViewBag.mensaje = &quot;Informacion Guardada&quot;;
              return View(&quot;Guardar&quot;,modelo);
          }
      }
  }
  </code></pre>
  <p>Code of <code>FacultyAdmin.cs</code>:</p>
  <pre class="lang-cs prettyprint-override"><code>using POS.Models;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace POS.Datos
  {
      public class FacultyAdmin
      {
          public void Guardar(Faculty modelo)
          {
             using (Cafeteria_POSEntities context = new Cafeteria_POSEntities())
              {
                  context.Faculties.Add(modelo);
                  context.SaveChanges();  
              }
          }
  
          public IEnumerable&lt;Faculty&gt;Consultar()
          {
              using (Cafeteria_POSEntities context = new Cafeteria_POSEntities())
              {
                  return context.Faculties.AsNoTracking().ToList(); //AsNoTracking para no hacer copia en  memoria porque no se hace ningun CRUD 
              }
          }
      }
  }
  </code></pre>
  <p>Faculties definition:</p>
  <pre class="lang-cs prettyprint-override"><code>// &lt;auto-generated&gt;
  //     This code was generated from a template.
  //
  //     Manual changes to this file may cause unexpected behavior in your application.
  //     Manual changes to this file will be overwritten if the code is regenerated.
  // &lt;/auto-generated&gt;
  //------------------------------------------------------------------------------
      
  using System;
  using System.ComponentModel.DataAnnotations;
  using System.Data.Entity;
  using System.Data.Entity.Infrastructure;
  
  namespace POS.Models
  {
      
      public partial class Cafeteria_POSEntities : DbContext
      {
          public Cafeteria_POSEntities()
              : base(&quot;name=Cafeteria_POSEntities&quot;)
          {
          }
      
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              throw new UnintentionalCodeFirstException();
          }
      
          public virtual DbSet&lt;Faculty&gt; Faculties { get; set; }
          public virtual DbSet&lt;KU_Students&gt; KU_Students { get; set; }
          public virtual DbSet&lt;Language_Students&gt; Language_Students { get; set; }
          public virtual DbSet&lt;Menu&gt; Menus { get; set; }
          public virtual DbSet&lt;Order&gt; Orders { get; set; }
          public virtual DbSet&lt;Plan&gt; Plans { get; set; }
          public virtual DbSet&lt;Station&gt; Stations { get; set; }
          public virtual DbSet&lt;sysdiagram&gt; sysdiagrams { get; set; }
      }
  }
  </code></pre>