title: EF Code First, exposing a linked table/object from a many to many relationship?
body: <p>I am trying to expose the linked class that EF code first automatically creates in many to many relationships as a separate object because <strong>that linked object needs to be referenced in other classes</strong>, however I seem to have a problem with getting the data that exists in the database.</p>
  
  <p>I've got the following 3 objects:</p>
  
  <pre><code> public class Role : Entity
  {
      public virtual ICollection&lt;User&gt; Users { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
      public virtual ICollection&lt;Permission&gt; Permissions { get; set; }
  }
  
  public class User: Entity
  {
      public string FirstName { get; set; }
      public string MiddleName { get; set; }
      public string LastName { get; set; }
  
      public virtual ICollection&lt;Role&gt; Roles { get; set; }
   }
  
   public class UserRole : Entity
  {
      public User User { get; set; }
      public Role Role { get; set; }
  }
  </code></pre>
  
  <p>This creates the following tables:</p>
  
  <p><img src="https://i.stack.imgur.com/FCPLS.png" alt="enter image description here"></p>
  
  <p>Now I can see the problem is that it is creating a <code>RoleUsers</code> table, when it shouldn't and should just use my <code>UserRoles</code> table. How can I force the linked table to be <code>UserRoles</code> so that I can have that linked object exposed in EF so I can use it in other objects?</p>
  
  <p>Also, how would I be traversing the objects then when writing queries? Would I still be able to use User.Roles.Any(y => y.Name == "blah"), or would I now have to query by going User.UserRoles.Any(y => y.Role.Name == "blah")? I suppose it isn't possible to maintain the transparent linking while simultaneously exposing the linked object?</p>
  
  <p>Edit: One of the classes that references UserRole as a navigation property looks like this:</p>
  
  <pre><code> public class UserRoleEntity : Entity
  {
      public UserRole UserRole { get; set; }
      public Guid EntityId { get; set; }
      public EntityType EntityType { get; set; }
  }
  </code></pre>
  
  <p>I don't want to individually store the User and Role object in this class because then they are not tightly coupled and data could be orphaned.</p>