加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

entity-framework – ASP.NET-Identity限制UserName长度

发布时间:2020-12-16 04:05:46 所属栏目:asp.Net 来源:网络整理
导读:如何限制表AspNetUsers中的UserName字段? 这不是: public class ApplicationUser : IdentityUser{ [Required,MaxLength(15)] public string UserName { get; set; }} 或这个: modelBuilder.Entity ApplicationUser().Property(x = x.UserName).HasMaxLeng
如何限制表AspNetUsers中的UserName字段?

这不是:

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

}

或这个:

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

作品.

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

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

为了详细,我试图设置这样的索引:

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

public override void Down()
{
    DropIndex("dbo.AspNetUsers","IX_UserName");
}

解决方法

在今天发布的最新版本中,这应该可以解决问题:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读