繁体   English   中英

指定的密钥太长; 最大密钥长度为767字节-ASPNet Identity MySQL

[英]Specified key was too long; max key length is 767 bytes - ASPNet Identity MySQL

我已经使用身份和MySQL创建了MVC应用程序。 我已经创建了实体,但是当我创建用户表时,它会失败,并出现标题中指定的错误。 我到处搜索,有人说UserNameNameEmail属性太长。 像下面的示例一样,我已经在这些列上设置了最大长度,但我一直无法使其正常工作。

IdentityModel:

 [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("CaptureDBContext", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<ApplicationUser>().Property(u => u.UserName).IsUnicode(false);
            modelBuilder.Entity<ApplicationUser>().Property(u => u.Email).IsUnicode(false);
            modelBuilder.Entity<IdentityRole>().Property(r => r.Name).HasMaxLength(255);
        }
    }

有人可以帮忙吗?

我可以将其与标准实体一起使用,但不能与身份表一起使用

这个问题不是重复的,因为链接中发布的教程已有2年的历史了,那时MySQL.Data版本不支持EF。 我正在使用的那个。

我自己找到了答案。

问题是与用户表中的用户名和电子邮件有关。 然后在角色表中的名称。

我在IdentityModel类中为这三个添加了最大长度。 这里是:

    [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("CaptureDBContext", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<ApplicationUser>().Property(u => u.UserName).HasMaxLength(255);
            modelBuilder.Entity<ApplicationUser>().Property(u => u.Email).HasMaxLength(255);
            modelBuilder.Entity<IdentityRole>().Property(r => r.Name).HasMaxLength(255);
        }
    }
}

哦,将DbConfigurationType添加为MySQL可以解决迁移历史记录表问题(我已经知道了)。

不幸的缪斯范

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM