Question

How do I restrict theUserName section of the tableAspNetUsers ?

But not that:

public class ApplicationUser : IdentityUser
{
    [Required, MaxLength(15)]
    public string UserName { get; set; }

}

and this

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

works.

I require this because I've configured anIndex on annvarchar(max) incorrect message that I receive:

Column 'UserName' in table 'dbo.AspNetUsers' is of a type that is invalid for use as a key column in an index.

I was attempting to set the indexes as follows in order to be verbose:

public override void Up()
{
    CreateIndex("dbo.AspNetUsers", "UserName", true, "IX_UserName");
}

public override void Down()
{
    DropIndex("dbo.AspNetUsers", "IX_UserName");
}
1
8
2/23/2014 10:47:20 PM

Accepted Answer

The most recent version made available today should work as follows:

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

5
3/20/2014 11:36:11 PM

Popular Answer

Even though a lot of time has gone, I believe it may still be helpful to someone. I experienced the similar issue and discovered a hint to my resolution here. The MaxLength attribute is ignored by the migration processes, although manual adjustments can be made:

public override void Up()
{
    AlterColumn("dbo.AspNetUsers", "UserName", c => c.String(nullable: false, maxLength: 15, storeType: "nvarchar"));
    CreateIndex("dbo.AspNetUsers", "UserName");
}

public override void Down()
{
    DropIndex("dbo.AspNetUsers", new[] { "UserName" });
    AlterColumn("dbo.AspNetUsers", "UserName", c => c.String(nullable: false, maxLength: 256, storeType: "nvarchar"));
}

After update-database Because the indexes are used to search effectively, the fields are condensed and the SQL queries searching by UserName execute more quickly (at least with mySQL, which I use).



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow