title: Entity Framework creating new entity with relationship to existing entity, results in attempt to create new copy of the existing entity
body: <p>I am trying to create a new user object with a specific Role.  The "Role" is an existing entity in EF.  I have googled, and stackoverflowed until I am blue in the face, and I have tried all the stuff that seems to be working for everyone else.  But when I try to save my new user object, it first tries to create a new "Role", instead of just creating the new user object with a reference to the existing Role.</p>
  
  <p>What am I doing wrong?</p>
  
  <pre><code>Role myRole = new Role { ID = myUser.Role.ID };
  myObjectContext.Roles.Attach(myRole);
  myUser.Role = myRole;
  
  if (myUser.ID == 0)
  {
      myObjectContext.Users.AddObject(myUser);
  }
  else
  {
      if (myUser.EntityState == System.Data.EntityState.Detached)
      {
          myObjectContext.Users.Attach(myUser);
      }
      myObjectContext.ObjectStateManager.ChangeObjectState(myUser, System.Data.EntityState.Modified);
  }
  myObjectContext.SaveChanges(SaveOptions.None);
  </code></pre>
  
  <p><strong>EDIT - AFTER MORE TESTING...</strong></p>
  
  <p>Ok.. so I have discovered some portion of the "cause" anyway.  I still don't know why it does this and need help.</p>
  
  <p>Basically, there are two sets of data I am attaching to my new User object.  One is the "Role" which is a FK to a Role table that contains the Role.  This shows up as a navigation property on the User like "User.Role".</p>
  
  <p>The second set of data is a collection of objects called "FIPS", which are a many-to-many relationship between the User and another table called FIPS.  There is a relationship table between them, that simply contains two columns, each a foreign key to User and FIPS, respectively.  The FIPS for a user are also a navigation property that is referenced like "User.FIPS".</p>
  
  <p>Here is the whole code showing the assignment of the FIPS and Role to the User object prior to saving the context.</p>
  
  <pre><code>List&lt;string&gt; fipsList = new List&lt;string&gt;();
  foreach (FIPS fips in myUser.FIPS)
  {
      fipsList.Add(fips.FIPS_Code);
  }
  myUser.FIPS.Clear();
  foreach (string fipsCode in fipsList)
  {
      FIPS myFIPS = new FIPS { FIPS_Code = fipsCode };
      myObjectContext.FIPSCodes.Attach(myFIPS);
      myUser.FIPS.Add(myFIPS);
  }
  
  
  Role myRole = new Role { ID = myUser.Role.ID };
  myObjectContext.Roles.Attach(myRole);
  myUser.Role = myRole;
  
  
  if (myUser.ID == 0)
  {
     myObjectContext.Users.AddObject(myUser);
  }
  else
  {
     if (myUser.EntityState == System.Data.EntityState.Detached)
     {
         myObjectContext.Users.Attach(myUser);
     }
     myObjectContext.ObjectStateManager.ChangeObjectState(myUser, System.Data.EntityState.Modified);
  }
  
  myObjectContext.SaveChanges(SaveOptions.None);
  </code></pre>
  
  <p>I set up my watch to check the status of "myObjectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added)" to see when things were being added to this.</p>
  
  <p>As soon as the first Related object is added to the User object, the second Related object that hasn't yet been attached to the context, is added to the context with an EntityState of "Added".</p>
  
  <p>.... Gonna see if there is a way to avoid attaching the related entities to the User entity until after they have all been attached to the context.</p>
  
  <p><strong>--FOLLOWUP--</strong>
  Ok.. well I changed the order of the code so that the related entities were attached to the context before being assigned to the User entity.. but as soon as the first related entity is assigned, the second related entity is shown as "added" in the ObjectStateEntries.
  So, then I changed it to the following order:</p>
  
  <ol>
  <li>Attach all related entities to context. </li>
  <li>Remove existing relationships on the user object to related entity
  types.</li>
  <li>Assign related entities to user entity.</li>
  <li>Save user entity.</li>
  </ol>
  
  <p>And.. now.. it works.. omg it works...  !  =)</p>