title: An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key
body: <p>Basically, I have a table which contains a few properties for a company. This is the "master" table and their ID is used in many other tables. I basically find their ID via this method:</p>
  
  <pre><code>private Company currentcompany()
      {
          Company cuco = db.Companies.Single(x =&gt; x.username == User.Identity.Name);
          return cuco;
      }
  </code></pre>
  
  <p>I need to give users the ability to update various details about themselves stored in this table, which I did perfectly well - however, I noticed a big security hole!</p>
  
  <p>Using Tamper Data on Firefox (And I imagine Fidler/many others), I could easily change the hidden ID and modify another companies details.</p>
  
  <p>To stop this, I added the following lines to the modify action:</p>
  
  <pre><code>        Company cuco = currentcompany();
  
          if (company.id != cuco.id)
          {
              return Content("Security Error");
          }
  </code></pre>
  
  <p>(FYI - <code>Company</code> is a model/POCO representing a company, and <code>company</code> itself is the form data.)</p>
  
  <p>After adding this, if I edit the ID in the form data, it works as expected and brings up "Security Error", however, if there isn't an error and I go on, I get the error in the question.</p>
  
  <p>"An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."</p>
  
  <p>I believe this is because EF is somehow detecting and keeping the first data pull, but I am just un sure on how to correct it.</p>
  
  <p>Any advice?</p>
  
  <p>edit-
  --update--</p>
  
  <p>If you can understand what I am trying to achieve, is there a better way of going around this?</p>