title: Referencing same Entity with different names in an Entity
body: <p>I have a model like:</p>
  
  <pre><code>public class Sample
  {
      [Key]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public Guid SampleId { get; set; }
  
      public virtual SampleProperty Saloon { get; set; }
      public virtual SampleProperty Room { get; set; }
      public virtual SampleProperty Balcony { get; set; }
  }
  </code></pre>
  
  <p>and a related entity:</p>
  
  <pre><code>public class SampleProperty
  {
      [Key]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
  
      public Piece Piece { get; set; }
      public FloorType Floor { get; set; }
      public WallType Wall { get; set; }
      public DoorType Door { get; set; }
      public WindowType Window { get; set; }
  }
  </code></pre>
  
  <p>and ofcourse related Enums for the types in SampleProperty Entity.</p>
  
  <p>Related controller is:</p>
  
  <pre><code>    [HttpPost]
      [ValidateAntiForgeryToken]
      public ActionResult Sample(Sample model)
      {
          if (ModelState.IsValid)
          {
              db.Entry(model.Saloon).State = EntityState.Modified;
              db.Entry(model.Room).State = EntityState.Modified;
              db.Entry(model.Balcony).State = EntityState.Modified;
              db.Entry(model).State = EntityState.Modified;
              db.SaveChanges();
              return RedirectToAction("Index");
          }
          return View(model);
      }
  </code></pre>
  
  <p>Controller during get operation is:</p>
  
  <pre><code>    [HttpGet]
      public ActionResult Sample(Guid? id)
      {
          if (id == null)
          {
              return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
          }
          Sample @sample = db.Samples.Where(x =&gt; x.SampleId == id).FirstOrDefault();
          if (@sample == null)
          {
              return HttpNotFound();
          }
          @sample.Room = new SampleProperties();
          @sample.Saloon = new SampleProperties();
          @sample.Balcony = new SampleProperties();
  
          return View(@sample);
      }
  </code></pre>
  
  <p>By the way; I am including following properties in View;</p>
  
  <pre><code>                        @Html.HiddenFor(model =&gt; model.Saloon.Id)
                          @Html.HiddenFor(model =&gt; model.Room.Id)
                          @Html.HiddenFor(model =&gt; model.Balcony.Id)
  </code></pre>
  
  <p>My problem is: When I try to update this entity. I am having error below. Do you have any idea?</p>
  
  <blockquote>
    <p>Attaching an entity of type
    'Project.Models.SampleProperties' failed because another
    entity of the same type already has the same primary key value. This
    can happen when using the 'Attach' method or setting the state of an
    entity to 'Unchanged' or 'Modified' if any entities in the graph have
    conflicting key values. This may be because some entities are new and
    have not yet received database-generated key values. In this case use
    the 'Add' method or the 'Added' entity state to track the graph and
    then set the state of non-new entities to 'Unchanged' or 'Modified' as
    appropriate.</p>
  </blockquote>