ASP.NET-Identity:如何限制用户名的长度? [英] ASP.NET-Identity: how to limit UserName length?

查看:52
本文介绍了ASP.NET-Identity:如何限制用户名的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何限制表 AspNetUsers UserName 字段的长度?

How can I limit the length of UserName field in the table AspNetUsers?

都不是:

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

}

或者这个:

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

有效.

我需要这个,因为在 nvarchar(max)上设置 Index 会给我以下错误消息:

I need this because setting an Index on an nvarchar(max) gives me this error message:

表"dbo.AspNetUsers"中的用户名"列的类型为无效,不能用作索引中的键列.

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);

这篇关于ASP.NET-Identity:如何限制用户名的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆