title: Entity Framework with MySql and Migrations failing because "max key length is 767 bytes"
body: <p><strong>[Edit]</strong> This problem was solved! See the instructions at the end of the post.</p>
  
  <p><strong>[Edit 2]</strong> Ok, this thread is old, and the newer versions of MySQL Connector already handle this with MySQL EF resolvers. Look for @KingPong answer on this thread. I haven't tested it, though.</p>
  
  <p>I'm trying to use MySql and EntityFramework with Migrations, but something seems to be wrong.</p>
  
  <p>When I enter <code>Update-Database -Verbose</code> in the Package Manager Console, EF executes some queries that will "mirror" my model classes, and everything goes perfect, BUT then EF tries to execute this query:</p>
  
  <pre><code>create table `__MigrationHistory` 
  (
    `MigrationId` varchar(150)  not null 
    ,`ContextKey` varchar(300)  not null 
    ,`Model` longblob not null 
    ,`ProductVersion` varchar(32)  not null
    ,primary key ( `MigrationId`,`ContextKey`) 
   ) engine=InnoDb auto_increment=0
  </code></pre>
  
  <p>And the result is: <code>Specified key was too long; max key length is 767 bytes</code></p>
  
  <p>I tried to change my database collation to utf-8, but still the same. Perhaps the key lenght is 450 characters, doing the UTF-8 math (which I may be wrong), I think it's trying to create a key around 1800 bytes length.</p>
  
  <p>Since I'm new to EF, I followed some tutorials and they told me to do this:</p>
  
  <pre><code>    public Configuration()
      {
          AutomaticMigrationsEnabled = false;
  
          SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
      }
  </code></pre>
  
  <p>Perhaps this SQL generator is doing the wrong thing, or EF itself is asking to the generator to make a key up to 767 bytes. </p>
  
  <p>How can I fix that, avoid this problem and get it to work with MySql?</p>
  
  <p><strong>[Edit]</strong>
  Ok, this problem was solved. You have to tell EF it has to change the way it generates the __MigrationHistory table.</p>
  
  <p>What I did:
  First, create a file called <code>MySqlHistoryContext.cs</code> (or whatever you want) with this content:</p>
  
  <pre><code>...
  using System.Data.Common;
  using System.Data.Entity;
  using System.Data.Entity.Migrations.History;
  
  
  namespace [YOUR NAMESPACE].Migrations //you can put any namespace here, but be sure you will put the corret using statement in the next file. Just create a new class :D
  {
      public class MySqlHistoryContext : HistoryContext
      {
  
          public MySqlHistoryContext(DbConnection connection, string defaultSchema):base(connection,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(); 
          }
      }
  }
  </code></pre>
  
  <p>You might have a file called <code>Configuration.cs</code> inside your <code>Migrations</code> folder. If yes, make the necessary adjustments, otherwise create a new file. Actually you kinda won't be able to get to this error if you didn't have this file, since EF creates it automatically when you <code>Add-Migration [name]</code>.</p>
  
  <pre><code>namespace [YOUR NAMESPACE].Migrations
  {
      using System;
      using System.Data.Entity;
      using System.Data.Entity.Migrations;
      using System.Linq;
  
      internal sealed class Configuration : DbMigrationsConfiguration&lt;CodeFirstMySql.Models.Context&gt;
      {
          public Configuration()
          {
              AutomaticMigrationsEnabled = false;
  
              SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator()); //it will generate MySql commands instead of SqlServer commands.
  
              SetHistoryContextFactory("MySql.Data.MySqlClient", (conn, schema) =&gt; new MySqlHistoryContext(conn, schema)); //here s the thing.
  
  
  
          }
  
          protected override void Seed(CodeFirstMySql.Models.Context context){}//ommited
      }
  }
  </code></pre>
  
  <p>Then <code>Update-Database -Verbose</code> and have fun!</p>