title: One to many relationship with junction table using Entity Framework code first
body: <p>I'm creating a database using EF6 code first, and am struggling to create a one-to-many relationship using a junction table.</p>
  
  <p>Here's an example of what I'm trying to do:</p>
  
  <p><code>Foo</code> entities can contain any number (0-n) of <code>Bar</code> entities, however <code>Bar</code> entities don't necessarily belong to a <code>Foo</code>. I may want a different type of entity to also contain one or more <code>Bar</code>, so it's important that <code>Bar</code> doesn't contain its parent's foreign key.</p>
  
  <p>So the join table would be like so:</p>
  
  <pre><code>Name        | FooBar
  ------------|-------
  Primary Key | BarID
  Key         | FooID
  </code></pre>
  
  <p>So if we create the entities as follows:</p>
  
  <pre><code>public class Foo
  {
    public long ID { get; set; }
    public ICollection&lt;Bar&gt; Bars { get; set; }
  }
  
  public class Bar
  {
    public long ID { get; set; }
  }
  </code></pre>
  
  <p>And then configure them:</p>
  
  <pre><code>public class FooConfiguration : EntityTypeConfiguration&lt;Foo&gt;
  {
    HasKey(p =&gt; p.ID);
    HasMany(p =&gt; p.Bars)
      .WithRequired()
      .Map(m =&gt; {
        m.ToTable("FooBar");
        m.MapKey("FooKey");
      });
  }
  </code></pre>
  
  <p>But this results in the following exception being thrown:</p>
  
  <p><code>An exception of type 'System.InvalidOperationException' occurred in mscorlib.dll but was not handled in user code. Additional information: The specified table 'FooBar' was not found in the model. Ensure that the table name has been correctly specified.</code></p>
  
  <p>Unfortunately I'm not sure what this means - do I need to create a seperate <code>FooBar</code> entity?</p>
  
  <p><strong>How can I configure these entities so that the join table is correctly created?</strong></p>
  
  <p>Thanks!</p>