title: Error "Value cannot be null. Parameter name: entity" when deleting entity object
body: <p>I'm getting a strange error when I try to delete an entity. I'm using Entity Framework 4, C# and ASP.NET MVC 3.</p>
  
  <p>Here's my controller and the function to delete an entity object:</p>
  
  <pre><code>public class EmployeesController : Controller
  {
      readonly IEmployeesRepository _employeesRepository;
  
      public EmployeesController()
      {
          _employeesRepository = new SqlEmployeesRepository();
      }
  
      public RedirectToRouteResult Delete(int id)
      {
          var employee = _employeesRepository.GetEmployee(id);
          _employeesRepository.DeleteEmployee(employee);
          TempData["message"] = employee.Name + " was deleted";
          return RedirectToAction("Index");
      }
  }
  </code></pre>
  
  <p>And the repository code:</p>
  
  <pre><code>public class SqlEmployeesRepository : IEmployeesRepository
  {
      private readonly MyDBEntities _entities;
  
      public SqlAgencyTypesRepository()
      {            
          _entities = new MyDBEntities();
      }
  
  
      public IQueryable&lt;Employee&gt; Employees
      {
          get { return _entities.Employees.AsQueryable(); }
      }
  
  
      public Employee GetEmployee(int id)
      {
          return Employees.FirstOrDefault(e =&gt; e.EmployeeID == id);
      }
  
  
      public void DeleteEmployee(Employee employee)
      {
          _entities.Employees.Context.DeleteObject(employee);
          _entities.Employees.Context.SaveChanges();
      }
  }
  </code></pre>
  
  <p>A couple weird things:</p>
  
  <ul>
  <li>The item does get deleted. An error occurs at the <code>DeleteObject</code> line, but it's still deleted.</li>
  <li><p>This doesn't happen on my local machine, only on production. The only difference between the two is the connection string.</p>
  
  <pre><code>&lt;!-- Local conn string --&gt;
  &lt;!--&lt;add name="MyDBEntities" connectionString="metadata=res://*/Entities.MyDB.csdl|res://*/Entities.MyDB.ssdl|res://*/Entities.MyDB.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MyDB.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True&amp;quot;" providerName="System.Data.EntityClient" /&gt;--&gt;
  
  &lt;!-- Production conn string --&gt;
  &lt;add name="MyDBEntities" connectionString="metadata=res://*/Entities.MyDB.csdl|res://*/Entities.MyDB.ssdl|res://*/Entities.MyDB.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;Server=mysite.com;Database=MyDB;User ID=***;Password=***;Trusted_Connection=True;Integrated Security=False;MultipleActiveResultSets=True&amp;quot;" providerName="System.Data.EntityClient" /&gt;
  </code></pre></li>
  </ul>
  
  <p>Anybody know what's happening here?</p>