title: Many to Many index creation convention
body: <p>My question is related to <a href="https://stackoverflow.com/questions/48645872/prevent-index-created-by-convention-on-many-to-many-table">this SO post</a>, but instead of preventing I want to understand what's the convention of creating an index, as I can't find anything.</p>
  <p>Given entities:</p>
  <pre><code>public class Entity1
  {
    public Guid Id {get;set;}
    public string Name {get;set;}
    public ICollection&lt;Entity1Entity2Link&gt; Links {get;set;}
  }
  
  public class Entity2
  {
    public Guid Id {get;set;}
    public string Name {get;set;}
    public ICollection&lt;Entity1Entity2Link&gt; Links {get;set;}
  }
  
  public class Entity1Entity2Link
  {
    public Guid Entity1Id {get;set;}
    public Entity1 Entity1 {get;set;}
  
    public Guid Entity2Id {get;set;}
    public Entity2 Entity2 {get;set;}
  }
  </code></pre>
  <p>And many to many relation setup:</p>
  <pre><code>modelBuilder
                  .Entity&lt;Entity1Entity2Link&gt;()
                  .HasKey(ofl =&gt; new
                  {
                      ofl.Entity1Id ,
                      ofl.Entity2Id 
                  });
  
              modelBuilder
                  .Entity&lt;Entity1Entity2Link&gt;()
                  .HasOne(ofl =&gt; ofl.Entity1)
                  .WithMany(e =&gt; e.Links)
                  .HasForeignKey(ofl =&gt; ofl.Entity1Id);
  
              modelBuilder
                  .Entity&lt;Entity1Entity2Link&gt;()
                  .HasOne(ofl =&gt; ofl.Entity2)
                  .WithMany(e =&gt; e.Links)
                  .HasForeignKey(ofl =&gt; ofl.Entity2Id);
  </code></pre>
  <p>The resulting migration has index on Entity2Id</p>
  <pre><code>// create table etc...
  
  migrationBuilder.CreateIndex(
      name: &quot;IX_Entity1Entity2Link_Entity2Id&quot;,
      table: &quot;Entity1Entity2Link&quot;,
      column: &quot;Entity2Id&quot;);
  </code></pre>
  <p>I'm looking to find what's the convention to create this index ? One convention I could find states:</p>
  <blockquote>
  <p>EF Core will always create indexes for foreign key and alternate keys.</p>
  </blockquote>
  <p>But then why I don't see another index on <code>Entity1Id</code> column ?</p>
  <p>Also I noticed I can swap index created on a column, by swapping column in composite key definition, e.g.</p>
  <pre><code>modelBuilder
      .Entity&lt;Entity1Entity2Link&gt;()
      .HasKey(ofl =&gt; new
      {
          ofl.Entity2Id,
          ofl.Entity1Id
      });
  </code></pre>
  <p>This will create an index on <code>Entity1Id</code> column:</p>
  <pre><code>migrationBuilder.CreateIndex(
      name: &quot;IX_Entity1Entity2Link_Entity1Id&quot;,
      table: &quot;Entity1Entity2Link&quot;,
      column: &quot;Entity1Id&quot;);
  </code></pre>
  <p>I'm using EF Core 3.1.2.</p>
  <pre><code>&lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore&quot; Version=&quot;3.1.2&quot; /&gt;
  &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.Design&quot; Version=&quot;3.1.2&quot; /&gt;
  &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.SqlServer&quot; Version=&quot;3.1.2&quot; /&gt;
  &lt;PackageReference Include=&quot;Microsoft.EntityFrameworkCore.Tools&quot; Version=&quot;3.1.2&quot; /&gt;
  </code></pre>