title: Entity Framework + MySQL: BLOB/TEXT column 'Hash' used in key specification without a key length
body: <p>I'm trying to use Entity Framework with MySQL. I've just installed <a href="http://dev.mysql.com/tech-resources/articles/mysql-installer-for-windows.html" rel="nofollow noreferrer">MySQL Installer for Windows</a> which includes MySQL Connector for .NET.</p>
  
  <p>I can't find any documentation on how to actually use this thing, so I've been cobbling pieces together from various tutorials. It appears I need the references for <code>MySql.Data</code> and <code>MySql.Data.Entity for EF6</code> (<a href="https://i.imgur.com/TFwVSNP.png" rel="nofollow noreferrer">screenshot</a>).</p>
  
  <p>Then I stole this source code from <a href="https://github.com/garvincasimir/csharp-parameterized-query-builder/blob/master/Parameterized%20Query%20Tests/TestDBConfig.cs" rel="nofollow noreferrer">some random project</a> which is the only reference I could find to <code>MySqlConnectionFactory</code>:</p>
  
  <pre><code>class ImgSigDbConfig : DbConfiguration
  {
      public ImgSigDbConfig()
      {
          base.AddDependencyResolver(new MySqlDependencyResolver());
          base.SetProviderFactory(MySqlProviderInvariantName.ProviderName, new MySqlClientFactory());
          base.SetProviderServices(MySqlProviderInvariantName.ProviderName, new MySqlProviderServices());
          base.SetDefaultConnectionFactory(new MySqlConnectionFactory());
          base.SetMigrationSqlGenerator(MySqlProviderInvariantName.ProviderName, () =&gt; new MySqlMigrationSqlGenerator());
          base.SetProviderFactoryResolver(new MySqlProviderFactoryResolver());
          base.SetManifestTokenResolver(new MySqlManifestTokenResolver());
      }
  }
  </code></pre>
  
  <p>I couldn't figure out how to set my database credentials anywher ein there, but <a href="https://stackoverflow.com/a/24223776/65387">apparently</a> you can pass them in when constructing the <code>DbContext</code>:</p>
  
  <pre><code>Database.SetInitializer(new DropCreateDatabaseIfModelChanges&lt;ImgSimContext&gt;());
  
  var conn = new MySqlConnectionStringBuilder
  {
      Server = "localhost",
      UserID = "root",
      Password = "pass",
      Database = "ImgSig"
  };
  
  
  using (var db = new ImgSimContext(conn.ToString()))
  {
      SqlConnection.ClearAllPools();
      db.Database.Initialize(force: true);
  </code></pre>
  
  <p>Now I've created a very simple POCO:</p>
  
  <pre><code>internal class ImgSig
  {
      [Key, Required]
      public int Id { get; set; }
  
      [Index, StringLength(16), Required]
      public byte[] Hash { get; set; }
  }
  </code></pre>
  
  <p>But when it hits <code>db.Database.Initialize</code> an exception is thrown:</p>
  
  <blockquote>
    <p>BLOB/TEXT column 'Hash' used in key specification without a key length</p>
  </blockquote>
  
  <p>Which makes perfect sense because in MySQL you have to set the key length for any blob/text fields (although it would be nice to see what SQL it's trying to run).</p>
  
  <p>So my questions are:</p>
  
  <ol>
  <li>How can I change the data type of <code>Hash</code> to a fixed length <code>binary(16)</code>?</li>
  <li>How can I set the key length (preferably using attributes)?</li>
  </ol>