title: Many to Many Mapping Using Data Annotations
body: <pre><code>[Table("UserMaster")]
  public class UserMaster  
  {
      public UserMaster()
      {
          this.Roles = new List&lt;Role&gt;();
      }
  
      [Key]    
      public int UserId { get; set; }
      public string UserName { get; set; }
      public ICollection&lt;Role&gt; Roles { get; set; }
   } 
  
  
  
  [Table("Role")]
  public class Role 
  {
      public Role()
      {
          this.Users = new List&lt;UserMaster&gt;();
      }
      public int RoleId{ get; set; }
      public string Name{ get; set; }
      public ICollection&lt;UserMaster&gt; Users { get; set; }
  
  }
  </code></pre>
  
  <p>I map these tables using EntityTypeConfiguration and it triggers OnModelCreate </p>
  
  <pre><code>this.HasMany(a =&gt; a.Roles).WithMany(b =&gt; b.Users).Map(m =&gt;
          {
              m.MapLeftKey("UserId");
              m.MapRightKey("RoleId");
              m.ToTable("UsersInRoles");
          });
  </code></pre>
  
  <p>I wonder If there Is a way to map them in the class Role or UserMaster. We can use                                   </p>
  
  <pre><code>[ForeignKey()]
  [Key]
  [Table()]
  </code></pre>
  
  <p>Could we do the mapping thing also?</p>