title: Identity: How to add items to related fields
body: <p>I want realise edit form, but error update item.
  This is small example.</p>
  
  <pre><code>public class AppUser : IdentityUser
  {
      public string SurName { get; set; }
      public virtual ICollection&lt;City&gt; Cities{ get; set; }
      public AppUser()
      {
          Cities = new List&lt;City&gt;();          
      }
  }
  </code></pre>
  
  <p>And the a model for the City</p>
  
  <pre><code>public class City
  {        
      public int Id { get; set; }
      public string Name { get; set; }
      public virtual ICollection&lt;AppUser&gt; Users { get; set; }
      public City()
      {
          Users = new List&lt;AppUser&gt;();
      }
  
  }
  </code></pre>
  
  <p>And the UserContext class  building up the one-to-many relationship between the 2 models.   </p>
  
  <pre><code>public class UserContext: IdentityDbContext&lt;AppUser&gt;
  {
      public DbSet&lt;City&gt; Cities { get; set; }
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;City&gt;().HasMany(x =&gt; x.Users)
              .WithMany(x =&gt; x.Cities)
              .Map(x =&gt; x.MapLeftKey("CityId")
              .MapRightKey("UserId")
              .ToTable("UserCity"));
          base.OnModelCreating(modelBuilder);
      }
  }   
  </code></pre>
  
  <p>And if I try update </p>
  
  <pre><code>UserContext db = new UserContext();
  ....
  public ActionResult Edit()
  {
   var newUser = UserManager.FindById(User.Identity.GetUserId());
   City city = db.Cities.FirstOrDefault();
   newUser.Cities = new List&lt;City&gt;() { city };
   newUser.SurName = "TestName";
   var result =  UserManager.Update(newUser);
   ....
  }
  </code></pre>
  
  <p>I see error</p>
  
  <pre><code>System.InvalidOperationException: 
  "The relationship between the two objects cannot be defined because 
   they are attached to different ObjectContext objects."
  </code></pre>
  
  <p>If delete line</p>
  
  <pre><code>newUser.Cities = new List&lt;City&gt;() { city };
  </code></pre>
  
  <p>Error not show.
  Why an error occurs when adding a field with many-to-many relationships?</p>