title: ASP.NET-Identity limit UserNameIndex length
body: <p>How can I limit the UserNameIndex UNIQUE index in the table AspNetUsers?</p>
  
  <p>I am using ASP.NET Identity with a Mysql backend and am running up against another instance of:</p>
  
  <pre><code>Index column size too large. The maximum column size is 767 bytes.
  </code></pre>
  
  <p>I have tried</p>
  
  <pre><code>modelBuilder.Entity&lt;Secuser&gt;().Property(x =&gt; x.UserName).HasMaxLength(100);
  modelBuilder.Entity&lt;Secuser&gt;().Property(t =&gt; t.Id).HasMaxLength(100);
  </code></pre>
  
  <p>I have done all the standard:</p>
  
  <pre><code>public class Secuser : IdentityUser
   {
   [Required]
   public string FullName { get; set; }
  
   [Required]
   public string Address { get; set; }
  
   [Required]
   public string City { get; set; }
  
   [Required]
   public string Country { get; set; }
  
   [Required]
   public bool AgreeTermsOfServicePrivacyPolicy { get; set; }
  
   [Required]
   public string BasePath { get; set; }
  }
  
  [DbConfigurationType(typeof(MySqlEFConfiguration))]
  public class ApplicationDbContext : IdentityDbContext&lt;Secuser&gt;
  {
   public ApplicationDbContext() : base("DefaultConnection")
   {
   }
   .....
   protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
    modelBuilder.Entity&lt;Secuser&gt;().Property(x =&gt; x.UserName).HasMaxLength(100);
    modelBuilder.Entity&lt;Secuser&gt;().Property(t =&gt; t.Id).HasMaxLength(100);
    .....
    base.OnModelCreating(modelBuilder);
    }
   }
  </code></pre>
  
  <p>When I issue:</p>
  
  <pre><code>update-database -verbose
  </code></pre>
  
  <p>the output is:</p>
  
  <pre><code>Using StartUp project 'sec'.
  Using NuGet project 'sec'.
  Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
  Target database is: 'sec' (DataSource: localhost, Provider: MySql.Data.MySqlClient, Origin: Configuration).
  Applying explicit migrations: [201405072209015_InitialCreate].
  Applying explicit migration: 201405072209015_InitialCreate.
  .....
  create table `AspNetUsers` (`Id` nvarchar(128)  not null ,`FullName` longtext not null ,`Address` longtext not null ,`City` longtext not null ,`Country` longtext not null ,`AgreeTermsOfServicePrivacyPolicy` bool not null ,`BasePath` longtext not null ,`Email` nvarchar(256) ,`EmailConfirmed` bool not null ,`PasswordHash` longtext,`SecurityStamp` longtext,`PhoneNumber` longtext,`PhoneNumberConfirmed` bool not null ,`TwoFactorEnabled` bool not null ,`LockoutEndDateUtc` datetime,`LockoutEnabled` bool not null ,`AccessFailedCount` int not null ,`UserName` nvarchar(256)  not null ,primary key ( `Id`) ) engine=InnoDb auto_increment=0
  CREATE UNIQUE index  `UserNameIndex` on `AspNetUsers` (`UserName` DESC) using HASH
  MySql.Data.MySqlClient.MySqlException (0x80004005): Index column size too large. The maximum column size is 767 bytes.
  at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
  at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32&amp; affectedRow, Int64&amp; insertedId)
  .....
  </code></pre>
  
  <p>I have also made the changes to allow Migration History to work:</p>
  
  <pre><code>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>It appears that the entire problem centers around</p>
  
  <pre><code>`UserName` nvarchar(256)
  
  modelBuilder.Entity&lt;Secuser&gt;().Property(x =&gt; x.UserName).HasMaxLength(100);
  </code></pre>
  
  <p>appears to not be working. I am using EntityFramework 6.1.0 and Microsoft.AspNet.Identity.Core 2.0.1.</p>
  
  <p>So, it appears from looking at:</p>
  
  <p><a href="https://stackoverflow.com/questions/21975717/asp-net-identity-limit-username-length">ASP.NET-Identity limit UserName length</a></p>
  
  <p>that either I am missing something in how I implemented HasMaxLength or there is some other reason why it does not work.</p>