title: How can I expose the Join table?
body: <p>I'm trying to follow this idea from <a href="http://lostechies.com/jimmybogard/2014/03/12/avoid-many-to-many-mappings-in-orms/" rel="nofollow" title="Avoid many-to-many mappings in ORMs">Jimmy Bogard</a> on avoiding Many-to-Many-Mappings in ORMs</p>
  
  <p>Given this setup I want to be able to also directly expose the 'Join' object in the relationship.</p>
  
  <p>Objects</p>
  
  <ul>
  <li>User</li>
  <li>Role</li>
  <li>UserRole (association object)</li>
  </ul>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>public class User
  {
      public Guid Id { get; set; }
      public string Name { get; set; }
      public virtual ICollection&lt;Role&gt; Roles { get; set; }
  }
  
  public class Role
  {
      public Guid Id { get; set; }
      public string Name { get; set; }
      public virtual ICollection&lt;User&gt; Users { get; set; }
  }
  
  public class UserRole
  {
      public Guid UserId { get; set; }
      public Guid RoleId { get; set; }
      public User User { get; set; }
      public Role Role { get; set; }
  }
  
  public class MyContext : DbContext 
  {
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          //modelBuilder.Entity&lt;UserRole&gt;().HasKey(u =&gt; new { u.RoleId, u.UserId });
          modelBuilder.Entity&lt;User&gt;().HasMany(x =&gt; x.Roles).WithMany(x =&gt; x.Users).Map(m =&gt;
          {
              m.ToTable("UserRoles");
              m.MapLeftKey("UserId");
              m.MapRightKey("RoleId");
          });
      }
  
      public DbSet&lt;User&gt; Users { get; set; }
      public DbSet&lt;Role&gt; Roles { get; set; }
      //public DbSet&lt;UserRole&gt; UserRoles { get; set; } 
  }
  </code></pre>
  
  <p>If I add a migration for this I get what I expect.</p>
  
  <pre><code>public override void Up()
  {
      CreateTable(
          "dbo.Roles",
          c =&gt; new
              {
                  Id = c.Guid(nullable: false),
                  Name = c.String(),
              })
          .PrimaryKey(t =&gt; t.Id);
  
      CreateTable(
          "dbo.Users",
          c =&gt; new
              {
                  Id = c.Guid(nullable: false),
                  Name = c.String(),
              })
          .PrimaryKey(t =&gt; t.Id);
  
      CreateTable(
          "dbo.UserRoles",
          c =&gt; new
              {
                  UserId = c.Guid(nullable: false),
                  RoleId = c.Guid(nullable: false),
              })
          .PrimaryKey(t =&gt; new { t.UserId, t.RoleId })
          .ForeignKey("dbo.Users", t =&gt; t.UserId, cascadeDelete: true)
          .ForeignKey("dbo.Roles", t =&gt; t.RoleId, cascadeDelete: true)
          .Index(t =&gt; t.UserId)
          .Index(t =&gt; t.RoleId);
  }
  </code></pre>
  
  <p>As soon as I add a DBset to the DbContext for the UserRoles object. EF can't find the PK for the UserRoles object.</p>
  
  <blockquote>
    <p>UserRoles: EntityType: EntitySet 'UserRoles' is based on type 'UserRole' that has no keys defined.</p>
  </blockquote>
  
  <p>I then try specifying the Key like this:</p>
  
  <pre><code>modelBuilder.Entity&lt;UserRole&gt;().HasKey(u =&gt; new { u.RoleId, u.UserId });
  </code></pre>
  
  <p>But EF doesn't know I want to use the same UserRoles table because it add's a 2nd table for that object.</p>
  
  <pre><code>CreateTable(
  "dbo.UserRoles1",
  c =&gt; new
      {
          RoleId = c.Guid(nullable: false),
          UserId = c.Guid(nullable: false),
      })
  .PrimaryKey(t =&gt; new { t.RoleId, t.UserId })
  .ForeignKey("dbo.Roles", t =&gt; t.RoleId, cascadeDelete: true)
  .ForeignKey("dbo.Users", t =&gt; t.UserId, cascadeDelete: true)
  .Index(t =&gt; t.RoleId)
  .Index(t =&gt; t.UserId);
  </code></pre>
  
  <p>How can I instruct the DbModelBuilder that I want to only use a single UserRole table?</p>
  
  <p>I have a demo .sln for this problem on <a href="https://github.com/ctrlShiftBryan/SOQuestion-ExposeAssociationTable" rel="nofollow">github</a> </p>