title: Switching to HasMany() and getting Sequence contains no matching element error
body: <p>I'm in a project where the entities were using an extra class to map the relationship between them, example:</p>
  
  <pre><code>public class MainEnt 
  {
      public int Id { get; set; }
      public string Description { get; set; }
  
      public ICollection&lt;MainEntAnotherEnt&gt; AnotherEnts{get;set;}
  }
  
  public class AnotherEnt 
  {
      public int Id { get; set; }
      public string Description { get; set; }
  }
  
  public class MainEntAnotherEnt
  {
      public int Id { get; set; }
      public int MainEntId{ get; set; }
      public int AnotherEntId{ get; set; }
  }
  </code></pre>
  
  <p>The project also uses auto migrations, so the MainEntAnotherEnt table is created explicitly in one of the migrations.</p>
  
  <pre><code>        CreateTable(
                 "dbo.MainEntAnotherEnt",
                 c =&gt; new
                 {
                     Id = c.Int(nullable: false, identity: true),
                     MainEntId = c.Int(nullable: false),
                     AnotherEntId = c.Int(nullable: false),
                 })
                 .PrimaryKey(t =&gt; new { t.Id })
                 .ForeignKey("dbo.AnotherEnt", t =&gt; t.AnotherEntId)
                 .ForeignKey("dbo.MainEnt", t =&gt; t.MainEntId)
                 .Index(t =&gt; t.MainEntId)
                 .Index(t =&gt; t.AnotherEntId);
  </code></pre>
  
  <p>I want to change this approach so we get rid of the MainEntAnotherEnt class, and we can do it using mapping, so I made these changes:</p>
  
  <pre><code>public class MainEnt 
  {
      public int Id { get; set; }
      public string Description { get; set; }
  
      public virtual ICollection&lt;AnotherEnt&gt; AnotherEnts{get;set;}
  }
  
  public class AnotherEnt 
  {
      public int Id { get; set; }
      public string Description { get; set; }
  
      public virtual ICollection&lt;MainEnt&gt; MainEnt {get;set;}
  }
  
  
     modelBuilder.Entity&lt;MainEnt &gt;()
              .HasMany(m =&gt; m.AnotherEnts)
              .WithMany(a =&gt; a.MainEnt)
              .Map(ma =&gt;
              {
                  ma.MapLeftKey("MainEntId");
                  ma.MapRightKey("AnotherEntId");
                  ma.ToTable("MainEntAnotherEnt");
              });
  </code></pre>
  
  <p>But I'm getting the error:</p>
  
  <blockquote>
    <p>[InvalidOperationException: Sequence contains no matching element]
       System.Linq.Enumerable.Single(IEnumerable`1 source, Func`2 predicate) +472<br>
       System.Data.Entity.Migrations.Infrastructure.d__70.MoveNext() +471<br>
       System.Linq.d__22`3.MoveNext() +355<br>
       System.Linq.d__63`1.MoveNext() +79<br>
       System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +504<br>
       System.Linq.Enumerable.ToList(IEnumerable`1 source) +70<br><br>
       System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.FindAssociationTypePairs(ICollection`1 entityTypePairs) +862<br>
       System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(ModelMetadata source, ModelMetadata target, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String sourceModelVersion, String targetModelVersion) +222<br>
       System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator, String sourceModelVersion, String targetModelVersion) +588<br>
       System.Data.Entity.Migrations.DbMigrator.IsModelOutOfDate(XDocument model, DbMigration lastMigration) +72<br>
       System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) +342<br>
       System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) +609<br>
       System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +609<br>
       System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +116<br>
       System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context) +156<br>
       System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +71<br>
       System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +482<br>
       System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +174<br>
       System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +273<br>
       System.Data.Entity.Database.Initialize(Boolean force) +80</p>
  </blockquote>
  
  <p>Any guidance with how to fix this will be very appreciate it! </p>