简体   繁体   中英

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

I have created an MVC Application with Identity and MySQL. 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. 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:

 [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. 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. 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).

Unlucky musefan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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