title: How do I map to a lookup table using Entity Framework Code First with Fluent API
body: <p>I'm new to asp.net mvc and entity framework code first apporach, I'm not that keen on databases either. I apologise in advance for wrong terminology or the way I understand things.</p>
  
  <p>Now to the question. I have the following models:<br>
  <strong>User Model</strong></p>
  
  <pre><code> public class User
  {
      public int UserId { get; set; }
      public string Username { get; set; }
      public string Password { get; set; }
  
      public int RoleId { get; set; }
  
      [ForeignKey("RoleId")]
      public virtual IEnumerable&lt;Role&gt; Roles { get; set; }
  }
  </code></pre>
  
  <p><strong>Role Model</strong></p>
  
  <pre><code> public class Role
  {
      public int RoleId { get; set; }
      public string RoleName { get; set; }       
  }
  </code></pre>
  
  <p>What i eventually want is a way to use Ef codefirst approach with the fluent API to map the UserId and RoleId to a User_Role table with a one-to-many relation, a user can have multiple roles:
  <img src="https://i.stack.imgur.com/MP3Tq.png" alt="User_Role lookup table"> </p>
  
  <p>I assume that what s done in <a href="https://stackoverflow.com/questions/7954738/entity-framework-mapping-to-lookup-table">this question</a> is the right approach, except that there the author used a many-to-many connection. I tried it that way but the part with u => u.users gives me an error(I assume thats because there is no users property in the model, so he answered his question but didn't update his question?)</p>
  
  <p>My question: <strong>What is the exact fluent api code to let Ef generate this table for me?</strong></p>
  
  <p>Things I'm unsure about: (Feel free to ignore)</p>
  
  <ul>
  <li>Is this the right approach of my problem?</li>
  <li>Once i have the lookup table, is this still the right way to declare my navigational property so i can later use it like user.Roles and retrieve their roles?</li>
  <li>where will be the RoleId in the User model be populated from, Roles table or User_Role?</li>
  <li>Has having an ID in the lookup table any use?</li>
  </ul>
  
  <p>Thanks in advance! I really appreciate your expertise.</p>