title: How do I update the entity with the foreign key?
body: <p>I have two entities as follows:  </p>
  
  <pre><code> public class Package
       {
              public int PackageId { get; set; }
              public int BusinessId { get; set; }
  
               public string Name { get; set; }
       }
  </code></pre>
  
  <p>And </p>
  
  <pre><code>   public class Business
       {
  
  
      public int BusinessId { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
      }
  </code></pre>
  
  <p>I want to edit the Package and update the related Business entity.  I’ve been trying the following:</p>
  
  <pre><code>    [HttpPost]
      public ActionResult Edit(Package package)
      {
  
          if (ModelState.IsValid)
          {
  
                  db.Packages.Attach(new Package { PackageId=package.PackageId
         });
  
  
                  db.Entry(package).State = EntityState.Modified;
  
                  db.SaveChanges();
  
  
              return RedirectToAction("Index");
          }
          ViewBag.GenreId = new SelectList(db.Businesses , "BusinessId", "Name", package.BusinessId);
          return View(package);
      }
  </code></pre>
  
  <p>The code stops here with this message:</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’ve been trying the example like the one here... <a href="http://blogs.msdn.com/b/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx" rel="nofollow">http://blogs.msdn.com/b/efdesign/archive/2009/03/16/foreign-keys-in-the-entity-framework.aspx</a>... but I’m not sure what I’m doing wrong.</p>
  
  <p>How do I update the entity with the foreign key? </p>