EF6 with MySQL. The given key was not present in the dictionary
<p>I have Asp.Net MVC 5 application using Code First Entity Framework 6 linked to MySQL database.</p>

<p>When I created the database first time, it works fine. But when I make a change to the model then add migration. An error shows after (Update-Database).</p>

<p>This is the error:</p>

<pre><code>        PM&gt; Update-Database 
        System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
           at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
           at MySql.Data.MySqlClient.MySqlConnectionStringBuilder.&lt;&gt;c.&lt;.cctor&gt;b__2_7(MySqlConnectionStringBuilder msb, MySqlConnectionStringOption sender)
           at MySql.Data.MySqlClient.MySqlConnectionStringBuilder.get_Item(String keyword)
           at MySql.Data.MySqlClient.MySqlConnectionStringBuilder.GetConnectionString(Boolean includePass)
           at MySql.Data.MySqlClient.MySqlConnection.get_ConnectionString()
           at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.&lt;GetConnectionString&gt;b__12(DbConnection t, DbConnectionInterceptionContext`1 c)
           at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget, TInterceptionContext, TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
           at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.GetConnectionString(DbConnection connection, DbInterceptionContext interceptionContext)
           at System.Data.Entity.Internal.InternalConnection.GetStoreConnectionString(DbConnection connection)
           at System.Data.Entity.Internal.InternalConnection.OnConnectionInitialized()
           at System.Data.Entity.Internal.EagerInternalConnection..ctor(DbContext context, DbConnection existingConnection, Boolean connectionOwned)
           at System.Data.Entity.DbContext..ctor(DbConnection existingConnection, Boolean contextOwnsConnection)
           at System.Data.Entity.Migrations.History.HistoryContext..ctor(DbConnection existingConnection, String defaultSchema)
           at MySql.Data.Entity.MySqlHistoryContext..ctor(DbConnection existingConnection, String defaultSchema)
           at code_first_mysql.Migrations.Configuration.&lt;&gt;c.&lt;.ctor&gt;b__0_0(DbConnection conn, String schema) in C:\Users\User\Documents\Tree\New Backend\code first mysql\Migrations\Configuration.cs:line 16
           at System.Data.Entity.Migrations.History.HistoryRepository.CreateContext(DbConnection connection, String schema)
           at System.Data.Entity.Migrations.History.HistoryRepository.&lt;GetUpgradeOperations&gt;d__16.MoveNext()
           at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source)
           at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
           at System.Data.Entity.Migrations.DbMigrator.&lt;&gt;c__DisplayClassc.&lt;Update&gt;b__b()
           at System.Data.Entity.Migrations.Infrastructure.MigratorScriptingDecorator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
           at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
           at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
           at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
           at System.Data.Entity.Migrations.Infrastructure.MigratorScriptingDecorator.ScriptUpdate(String sourceMigration, String targetMigration)
           at System.Data.Entity.Migrations.Design.ToolingFacade.ScriptUpdateRunner.Run()
           at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
           at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
           at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
           at System.Data.Entity.Migrations.Design.ToolingFacade.ScriptUpdate(String sourceMigration, String targetMigration, Boolean force)
           at System.Data.Entity.Migrations.UpdateDatabaseCommand.&lt;&gt;c__DisplayClass2.&lt;.ctor&gt;b__0()
           at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
        The given key was not present in the dictionary.
</code></pre>

<p>These are my MySQL Configuration Classes:</p>

<pre><code>internal sealed class Configuration : DbMigrationsConfiguration&lt;ApplicationDbContext&gt;
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        SetSqlGenerator("MySql.Data.MySqlClient", new MySqlMigrationSqlGenerator());
        SetHistoryContextFactory("MySql.Data.MySqlClient", (conn, schema) =&gt; new MySqlHistoryContext(conn, schema));
        CodeGenerator = new MySqlMigrationCodeGenerator();
    }

    protected override void Seed(ApplicationDbContext context)
    {}
}


public class MySqlConfiguration: DbConfiguration
{

    public MySqlConfiguration()
    {
        SetHistoryContext("MySql.Data.MySqlClient", (conn, schema) =&gt; new MySqlHistoryContext(conn, schema));
    }

}



public class MySqlHistoryContext : HistoryContext 
{
    public MySqlHistoryContext(DbConnection existingConnection, string defaultSchema) : base(existingConnection, defaultSchema)
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity&lt;HistoryRow&gt;().Property(h =&gt; h.MigrationId).HasMaxLength(100).IsRequired();
        modelBuilder.Entity&lt;HistoryRow&gt;().Property(h =&gt; h.ContextKey).HasMaxLength(200).IsRequired();
    }
}

 public class MySqlInitializer : IDatabaseInitializer&lt;ApplicationDbContext&gt;
{

    public void InitializeDatabase(ApplicationDbContext context)
    {
        if (!context.Database.Exists())
        {
            // if database did not exist before - create it
            context.Database.Create();
        }
        else
        {
            // query to check if MigrationHistory table is present in the database 
            var migrationHistoryTableExists = ((IObjectContextAdapter)context).ObjectContext.ExecuteStoreQuery&lt;int&gt;(
            string.Format(
              "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '{0}' AND table_name = '__MigrationHistory'", "mydb"));

            // if MigrationHistory table is not there (which is the case first time we run) - create it
            if (migrationHistoryTableExists.FirstOrDefault() == 0)
            {
                context.Database.Delete();
                context.Database.Create();
            }
        }
    }
}
</code></pre>