title: EF CF m-to-n relationship via data annotations
body: <p>I want to map a many to may relationship using ONLY data annotations so this is my case.</p>
  
  <p>Say I have the following table</p>
  
  <pre><code>Foo:
  FooId int identity
  FooName varchar(max) not null
  
  Bar:
  BarId int identity
  BarName varchar(max) not null
  
  FooBarRelationships
  TheBarId int 
  TheFooId int 
  </code></pre>
  
  <p>As you can see the junction table contains FK names different from source. In addition the name of the table is also different</p>
  
  <p>My classes are as follows</p>
  
  <pre><code>[Table("Foo")]
  public class Foo {
      [Key]
      public Int32 FooId { get; set; }
      public String FooName { get; set; }
  
      public IEnumerable&lt;Bar&gt; Bars { get; set; }
  }
  
  [Table("Bar")]
  public class Bar { 
      [Key]
      public Int32 BarId { get; set; }
      public String BarName { get; set; }
  
      public IEnumerable&lt;Foo&gt; Foos { get; set; }
  }
  </code></pre>
  
  <p>How should I modify my classes using data annotations in order to tell that the table <code>FooBarRelationships</code> is a junction table and <code>TheBarId</code> is the foreign key from <code>Bar</code> table and <code>TheFooId</code> is the foreign key from <code>Foo</code> table?</p>
  
  <p><b>
  P.S. 
  </b></p>
  
  <p>Having navigation properties declared as <code>IEnumerable&lt;T&gt;</code>, not as <code>ICollection&lt;T&gt;</code> is essential. </p>
  
  <p>I've tried to map it using the map class which looks as follows</p>
  
  <pre><code>public class BarMap : EntityTypeConfiguration&lt;Bar&gt; {
      public BarMap() {
          this.HasMany(t =&gt; t.Foos)
              .WithMany(t =&gt; t.Bars)
              .Map(m =&gt; {
                  m.ToTable("FooBarRelationships");
                  m.MapLeftKey("TheBarId");
                  m.MapRightKey("TheFooId");
              });
      }
  }
  </code></pre>
  
  <p>this gives me the following error.</p>
  
  <blockquote>
    <p>The type arguments for method 'System.Data.Entity.ModelConfiguration.EntityTypeConfiguration&lt;Bar&gt;.HasMany&lt;TTargetEntity&gt; (System.Linq.Expressions.Expression&lt;System.Func&lt;Bar, ICollection&lt;TTargetEntity&gt;&gt;&gt;)' cannot be inferred from the usage. Try specifying the type arguments explicitly.</p>
  </blockquote>
  
  <p>If there is a way to resolve this error without changing the type <code>IEnumeralbe&lt;T&gt;</code> to <code>ICollection&lt;T&gt;</code>, it is also welcomed. </p>