title: Entity framework (code first), many to many, users and roles
body: <p>I got MVC 3 application with custom MembershipProvider, so it stores newly registered users to my database.
  I'm using Code First approach with existing database (I've used entity framework power tools for this). Got tables Users, Roles and UsersRoles.
  In table Users I got:
  UserID (PK)
  Username
  Password
  Email
  ...</p>
  
  <p>In table Roles I got
  RoleID (PK)
  Name
  Description</p>
  
  <p>In table UsersRoles  I got
  UserID (set as composite PK)
  RoleID (set as composite PK)</p>
  
  <pre><code> public partial class User
   {
      public User()
      {
          this.Roles = new List&lt;Role&gt;();
      }
  
      public int UserID { get; set; }
      public string Username { get; set; }
      public string Password { get; set; }
      public string Email { get; set; }
      public virtual ICollection&lt;Role&gt; Roles { get; set; }
  }
  
  public partial class Role
  {
      public Role()
      {
          this.Users = new List&lt;User&gt;();
      }
      public int RoleID { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
      public virtual ICollection&lt;User&gt; Users { get; set; }
  }
  
  
  public class UserMap : EntityTypeConfiguration&lt;User&gt;
  {
      public UserMap()
      {
          // Primary Key
          this.HasKey(t =&gt; t.UserID);
  
          // Properties
          this.Property(t =&gt; t.Username)
              .IsRequired()
              .HasMaxLength(20);
  
          this.Property(t =&gt; t.Password)
              .IsRequired()
              .HasMaxLength(50);
  
          this.Property(t =&gt; t.Email)
              .IsRequired()
              .HasMaxLength(100);
  
  
  
          // Table &amp; Column Mappings
          this.ToTable("Users");
          this.Property(t =&gt; t.UserID).HasColumnName("UserID");
          this.Property(t =&gt; t.Username).HasColumnName("Username");
          this.Property(t =&gt; t.Password).HasColumnName("Password");
          this.Property(t =&gt; t.Email).HasColumnName("Email");
  
      }
  }
  
  public class RoleMap : EntityTypeConfiguration&lt;Role&gt;
  {
      public RoleMap()
      {
          // Primary Key
          this.HasKey(t =&gt; t.RoleID);
  
          // Properties
          this.Property(t =&gt; t.Name)
              .IsRequired()
              .HasMaxLength(20);
  
          this.Property(t =&gt; t.Description)
              .HasMaxLength(50);
  
          // Table &amp; Column Mappings
          this.ToTable("Roles");
          this.Property(t =&gt; t.RoleID).HasColumnName("RoleID");
          this.Property(t =&gt; t.Name).HasColumnName("Name");
          this.Property(t =&gt; t.Description).HasColumnName("Description");
  
          // Relationships
          this.HasMany(t =&gt; t.Users)
              .WithMany(t =&gt; t.Roles)
              .Map(m =&gt;
                  {
                      m.ToTable("UsersRoles");
                      m.MapLeftKey("RoleID");
                      m.MapRightKey("UserID");
                  });
  
  
      }
  }
  </code></pre>
  
  <p>I have predefined roles in Roles table and I don't want them to be modified when new users registers (Roles table should be only modified by administrator), I want new user to be stored in Users table and assign him default role of a regular user (with no special privileges), so it adds a record to Users table + a record in UsersRoles table. </p>
  
  <pre><code>   public void AddUser(User user)
      {   
          Users.Add(user);
          SaveChanges();
     }
  
   var tmp = new User { Username = username, Password = GetMd5Hash(password), Email = email };
  
  
              using (var db = new mycontext())
              {
                  mycontext.AddUser(userObj);
              }
  </code></pre>
  
  <p>But I have no idea how to make it add a new record to UsersRoles table..with UserID and RoleID (default one, the first one RoleID=1 - normal user privileges). Any help will be appreciated.</p>