ASP.NET-Identity: how to limit UserName length?

asp.net-identity, ef-code-first, entity-framework, entity-framework-6

Solution

In the latest version released today, this should do the trick:

`modelBuilder.Entity<ApplicationUser>().Property(x => x.UserName).HasMaxLength(15);`

Problem

How can I limit the length of `UserName` field in the table `AspNetUsers`? Neither this: ``` public class ApplicationUser : IdentityUser { [Required, MaxLength(15)] public string UserName { get; set; } } ``` or this: `modelBuilder.Entity<ApplicationUser>().Property(x => x.UserName).HasMaxLength(15);` works. I need this because setting an `Index` on an `nvarchar(max)` gives me this error message: Column 'UserName' in table 'dbo.AspNetUsers' is of a type that is invalid for use as a key column in an index. To be verbose, I was trying to set the indexes like this: ``` public override void Up() { CreateIndex("dbo.AspNetUsers", "UserName", true, "IX_UserName"); } public override void Down() { DropIndex("dbo.AspNetUsers", "IX_UserName"); } ```

Original source