title: Mapping Join Tables In The Entity Framework
body: <p>I am trying connect the asp.net membership tables in to an asp.mvc 3 website. I have been following the tutorial in Steve Sanderson's book 'Pro ASP.NET MVC 3 Framework' for the sport store and applying this to the tables that are generated from the membership exe.</p>
  
  <p>So I have a user class that looks like:</p>
  
  <pre><code>namespace Domain.Entities
  {
      public class User
      {
          public Guid UserId { get; set; }
          public string UserName { get; set; }
          public DateTime LastActivityDate;
          public virtual ICollection&lt;Role&gt; Roles { get; set; }
      }
  
      public class Role
      {
          public Guid RoleId { get; set; }
          public string RoleName { get; set; }
      }
  }
  </code></pre>
  
  <p>and a context class that looks like:</p>
  
  <pre><code>public class EFDbContext : DbContext
  {
      public DbSet&lt;User&gt; Users { get; set; }
      public DbSet&lt;Role&gt; Roles { get; set; }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;User&gt;().ToTable("aspnet_users");
          modelBuilder.Entity&lt;Role&gt;().ToTable("aspnet_roles");
      }
  }
  </code></pre>
  
  <p>but I get an error because I assume it's looking for a join between these two tables when in fact there is a join table (<code>aspnet_UsersInRoles</code>) in between to avoid a many to many link, when I try and reference the Role model from the User, such as:</p>
  
  <pre><code>var test = _repository.Users.FirstOrDefault().Roles.Count();
  </code></pre>
  
  <blockquote>
    <p>{"Invalid column name 'User_UserId'.\r\nInvalid column name
    'User_UserId'.\r\nInvalid column name 'User_UserId'."}</p>
  </blockquote>
  
  <p>is there a way to map the table together using the join table using the entity framework? Is it better just to add a new ADO.NET entity data model and let visual studio just reverse engineer the database?</p>