Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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");
}
like image 685
Yustme Avatar asked Feb 23 '14 22:02

Yustme


1 Answers

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

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

like image 132
Yustme Avatar answered Oct 07 '22 20:10

Yustme