Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Entity Framework issue - Specified key was too long; max key length is 3072 bytes

When I use this command:

$ dotnet ef database update

I get this error:

Specified key was too long; max key length is 3072 bytes

The table which cause this error is a built-in Microsoft table for user management:

CREATE TABLE `AspNetUserLogins` 
(
    `LoginProvider` varchar(767) NOT NULL,
    `ProviderKey` varchar(767) NOT NULL,
    `ProviderDisplayName` text NULL,
    `UserId` varchar(767) NOT NULL,

    PRIMARY KEY (`LoginProvider`, `ProviderKey`),
    CONSTRAINT `FK_AspNetUserLogins_AspNetUsers_UserId` 
        FOREIGN KEY (`UserId`) REFERENCES `AspNetUsers` (`Id`) 
        ON DELETE CASCADE
);

I tried to add this to OnModelCreating:

modelBuilder.Entity<Utilisateurs>(entity => entity.Property(m => m.NormalizedEmail).HasMaxLength(200));
modelBuilder.Entity<Utilisateurs>(entity => entity.Property(m => m.NormalizedUserName).HasMaxLength(200));
modelBuilder.Entity<IdentityRole>(entity => entity.Property(m => m.NormalizedName).HasMaxLength(200));
modelBuilder.Entity<IdentityUserLogin<string>>(entity => entity.Property(m => m.UserId).HasMaxLength(200));
modelBuilder.Entity<IdentityUserRole<string>>(entity => entity.Property(m => m.UserId).HasMaxLength(200));
modelBuilder.Entity<IdentityUserRole<string>>(entity => entity.Property(m => m.RoleId).HasMaxLength(200));
modelBuilder.Entity<IdentityUserToken<string>>(entity => entity.Property(m => m.UserId).HasMaxLength(200));
modelBuilder.Entity<IdentityUserClaim<string>>(entity => entity.Property(m => m.UserId).HasMaxLength(200));
modelBuilder.Entity<IdentityRoleClaim<string>>(entity => entity.Property(m => m.RoleId).HasMaxLength(200));

But it does not work anymore.

Thanks for your help

** EDIT **

I also tried:

modelBuilder.Entity<IdentityUserLogin<string>>(entity => entity.Property(m => m.LoginProvider).HasMaxLength(200));
modelBuilder.Entity<IdentityUserLogin<string>>(entity => entity.Property(m => m.ProviderKey).HasMaxLength(200));
like image 657
Bob5421 Avatar asked Jan 22 '18 19:01

Bob5421


1 Answers

I used official MySql provider MySql.Data.EntityFrameworkCore which is in pre-release state and has some troubles with migrations. Then I changed to Pomelo.EntityFrameworkCore.MySql and updated database without any errors.

like image 146
Damir Shakenov Avatar answered Oct 26 '22 18:10

Damir Shakenov