title: EF 4 Feature CTP 4 - Associate Many to Many
body: <p>I am using EF 4 Feature CTP 4.</p>
  
  <p>I have the following database schema: </p>
  
  <p>[Users] 1-M [UserRoles] M-1 [Roles]</p>
  
  <p>I have a User and Role class (both POCOs).</p>
  
  <p>When I try to associate an existing role to a user, a new record is getting inserted in the Roles table, instead of only inserting a new record in UserRoles.</p>
  
  <p>So say I have User 1 and want to Associate with Role 2. When I try to save, I end up with a new record in Roles named "Role 2" with a record in UserRoles to the newly created Role. From the code below I was expecting only a new record in UserRoles with the mapping between User and Role.</p>
  
  <p>Here is my POCO classes, mapping and test.</p>
  
  <p>POCO Classes:</p>
  
  <pre><code>public class User {
          public int Id { get; set; }
          public string FirstName { get; set; }
          public string LastName { get; set; }
          public byte[] DataVersion { get; set; }
  
          private List&lt;Role&gt; roles = new List&lt;Role&gt;();
          public virtual IList&lt;Role&gt; Roles {
              get {
                  return roles;
              }
          }   
      }
  
  public class Role {
          public int Id { get; set; }
          public string Name { get; set; }
  
          public List&lt;User&gt; Users { get; set; }
      }
  </code></pre>
  
  <p>Mapping:</p>
  
  <pre><code>public class UserConfiguration : EntityConfiguration&lt;Domain.User&gt; {
          public UserConfiguration() {
              this.MapSingleType(user =&gt; new {
                  UserId = user.Id,
                  FirstName = user.FirstName,
                  LastName = user.LastName,
                  DataVersion = user.DataVersion
              });
  
              this.Property(u =&gt; u.DataVersion).IsConcurrencyToken().HasStoreType("timestamp").StoreGeneratedPattern = StoreGeneratedPattern.Computed;
  
              //Users &lt;--&gt; Roles
              this.HasMany(u =&gt; u.Roles).WithMany(r =&gt; r.Users)
                  .Map("UserRoles", (u, r) =&gt; new {
                      UserId = u.Id,
                      RoleId = r.Id
                  });  
          }
     }
  
  public class RoleConfiguration : EntityConfiguration&lt;Domain.Role&gt; {
          public RoleConfiguration() {
              this.MapSingleType(role =&gt; new {
                  RoleId = role.Id,
                  RoleName = role.Name
              });
  
          }
      }
  </code></pre>
  
  <p>Test:</p>
  
  <pre><code>public void AssignRoleTest() {
              var userId = 1;
              User user;
  
              var userRepo = new UsersRepository();
              user = userRepo.GetUser(userId);
  
              var roleRepo = new RolesRepository();
  
              var roleId = 2;
              var role = roleRepo.GetRole(roleId);
  
              user.Roles.Add(role);
  
              userRepo.SaveUser(user);
  
       }
  </code></pre>
  
  <p>Code for Repository Save:</p>
  
  <pre><code>public void SaveUser(User user) {
          if (user.Id &gt; 0) {
              dbContext.Users.Attach(user);
              dbContext.MarkAsModified(user);
          }
          else {
              dbContext.Users.Add(user);
          }
  </code></pre>