title: Code First Many to Many : how to add a collection to an object
body: <p>I have a Many to Many relationship between User and Role.  They are set up as follows :</p>
  
  <pre><code>    public partial class User
      {
          //public User()
          //{
          //    //this.DateCreated = DateTime.Now; //set default value
          //    Roles = new HashSet&lt;Role&gt;();
          //}
  
          public ICollection&lt;Role&gt; Roles { get; set; } //many to many relationship
  
          public int UserId { get; set; }
          public string FirstName { get; set; }
          public string Surname { get; set; }
          public string Username { get; set; }
          public string Password { get; set; }
          public string City { get; set; }
  
          //foreign key
          public int CountryId { get; set; }
          //navigation properties
          public virtual Country Country { get; set; }
  
          //foreign key
          public int LanguageId { get; set; }
          //navigation properties
          public virtual Language Language { get; set; }
  
          public string EmailAddress { get; set; }
          public long? FacebookId { get; set; }
          public DateTime DateCreated { get; set; }
      }
  
  public partial class Role
      {
          //public Role()
          //{
          //    Users = new HashSet&lt;User&gt;();
          //}
  
          public ICollection&lt;User&gt; Users { get; set; } //many to many relationship
  
          public int RoleId { get; set; }
          public string RoleName { get; set; }
      }
  
  //many to many relationship 
              modelBuilder.Entity&lt;User&gt;().
                HasMany(c =&gt; c.Roles).
                WithMany(p =&gt; p.Users).
                Map(
                 m =&gt;
                 {
                     m.MapLeftKey("UserId");
                     m.MapRightKey("RoleId");
                     m.ToTable("UserRoles", schemaName: "Main");
                 });
  </code></pre>
  
  <p>In my code where I add a new user, I want to be able to add Roles to that user.  But whenever I do this, new Roles are also added to the Roles table.  What is the correct way to do this?</p>
  
  <pre><code>[HttpPost]
          public ActionResult UserAdd(UserDTO user)
          {
              if (ModelState.IsValid)
              {
                  //do mapping manually here
                  Country country = _repository.GetCountryByCountryId(user.CountryId);
                  Language language = _repository.GetLanguageByLanguageId(user.LanguageId);
  
                  User entity = new User();
                  entity.FirstName = user.FirstName;
                  entity.Surname = user.Surname;
                  entity.Username = user.Username;
                  entity.Password = user.Password;
                  entity.City = user.City;
                  entity.CountryId = country.CountryId;
                  entity.LanguageId = language.LanguageId;
                  entity.Country = country;
                  entity.Language = language;
                  entity.EmailAddress = user.EmailAddress;
                  entity.FacebookId = null;
                  entity.DateCreated = DateTime.Now;
                  entity.Roles = new List&lt;Role&gt;();
  
                  foreach (int i in user.Roles)
                  {
                      Role role = _repository.GetRoleByRoleId(i);
                      entity.Roles.Add(new Role { RoleId = i, RoleName = role.RoleName });
                  }
  
                  int newUserId = _repository.AddUser(entity);
  
                  return View();
              } }
  </code></pre>