简体   繁体   English

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

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

I have created an MVC Application with Identity and MySQL. 我已经使用身份和MySQL创建了MVC应用程序。 I have created my entities but when I come to creating the user table it fails with the error specified in the title. 我已经创建了实体,但是当我创建用户表时,它会失败,并出现标题中指定的错误。 I have searched around and people have said that the UserName , Name and Email properties are too long. 我到处搜索,有人说UserNameNameEmail属性太长。 I've tired to set a max length on these columns like the below example but I haven't been able to get it to work. 像下面的示例一样,我已经在这些列上设置了最大长度,但我一直无法使其正常工作。

IdentityModel: 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);
        }
    }

Can anyone help? 有人可以帮忙吗?

I can get it to work with standard entities but not the Identity tables 我可以将其与标准实体一起使用,但不能与身份表一起使用

This question is not a duplicate because the tutorial which is posted in the link is 2 years old, the MySQL.Data version then did not support EF. 这个问题不是重复的,因为链接中发布的教程已有2年的历史了,那时MySQL.Data版本不支持EF。 The one I am using does. 我正在使用的那个。

I found the answer myself. 我自己找到了答案。

The issue was to do with the UserName and Email in the User table. 问题是与用户表中的用户名和电子邮件有关。 And then the Name in the Role table. 然后在角色表中的名称。

I added a max length for all three in my IdentityModel class. 我在IdentityModel类中为这三个添加了最大长度。 Here it is: 这里是:

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

Oh, and adding the DbConfigurationType to be MySQL sorts out the migration history table issue(I already knew this). 哦,将DbConfigurationType添加为MySQL可以解决迁移历史记录表问题(我已经知道了)。

Unlucky musefan 不幸的缪斯范

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

相关问题 指定密钥太长; 最大密钥长度为767字节实体框架6中的Mysql错误 - Specified key was too long; max key length is 767 bytes Mysql error in Entity Framework 6 asp.net 显示指定密钥的核心 2 太长; 最大密钥长度为 3072 字节 - asp.net core 2 showing specified key was too long; max key length is 3072 bytes 实体框架“指定的键太长; 最大密钥长度为 1000 字节” - Entity Framework “Specified key was too long; max key length is 1000 bytes” 带有 MySql 和迁移的实体框架失败,因为“最大密钥长度为 767 字节” - Entity Framework with MySql and Migrations failing because "max key length is 767 bytes" 身份(在创建数据库时)错误指定的密钥太长 - Identity (on database creation) error specified key was too long Microsoft.AspNet.Identity无法定义INT主键? - Microsoft.AspNet.Identity can't define a INT primary key? Microsoft.AspNet.Identity.EntityFramework.IdentityUser 的外键? - Foreign Key To Microsoft.AspNet.Identity.EntityFramework.IdentityUser? 指定的令牌太长。 最大长度为128个字符 - The token specified is too long. The maximum length is 128 characters “应用程序配置”文件中的最大键/值长度 - Max key/value length in 'application configuration' files Unity中不对称加密的最大长度和消息太长错误 - Max Length and Message Too Long error for Asymmetric Encryption in Unity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM