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");
}
In the latest version released today, this should do the trick:
modelBuilder.Entity<ApplicationUser>().Property(x => x.UserName).HasMaxLength(15);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With