1
resposta

[Bug] Problema no update após configurar o identity na aula 02

Preciso de ajudo, após efetuar e migratio e tentar efetuar o update o seguinte erro é retornado:

fail: Microsoft.EntityFrameworkCore.Database.Command[20102] Failed executing DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] CREATE TABLE AspNetUserLogins ( LoginProvider varchar(767) NOT NULL, ProviderKey varchar(767) NOT NULL, ProviderDisplayName text NULL, UserId int NOT NULL, PRIMARY KEY (LoginProvider, ProviderKey), CONSTRAINT FK_AspNetUserLogins_AspNetUsers_UserId FOREIGN KEY (UserId) REFERENCES AspNetUsers (Id) ON DELETE CASCADE ); Failed executing DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] CREATE TABLE AspNetUserLogins ( LoginProvider varchar(767) NOT NULL, ProviderKey varchar(767) NOT NULL, ProviderDisplayName text NULL, UserId int NOT NULL, PRIMARY KEY (LoginProvider, ProviderKey), CONSTRAINT FK_AspNetUserLogins_AspNetUsers_UserId FOREIGN KEY (UserId) REFERENCES AspNetUsers (Id) ON DELETE CASCADE ); MySql.Data.MySqlClient.MySqlException (0x80004005): Specified key was too long; max key length is 3072 bytes at MySql.Data.MySqlClient.MySqlStream.ReadPacket() at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId) at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) at MySql.Data.MySqlClient.MySqlDataReader.NextResult() at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject) at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary2 parameterValues) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable1 migrationCommands, IRelationalConnection connection) at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) Specified key was too long; max key length is 3072 bytes

1 resposta

Olá Nathan

Pelo erro que você está recebendo, parece que o problema está relacionado ao tamanho da chave primária na tabela AspNetUserLogins. O erro específico é "Specified key was too long; max key length is 3072 bytes". Isso indica que a chave primária está excedendo o tamanho máximo permitido.

Uma possível solução para esse problema é reduzir o tamanho da chave primária. Você pode fazer isso alterando o tipo de dado das colunas LoginProvider e ProviderKey para um tipo de dado que tenha um tamanho menor, como varchar(255) por exemplo. Aqui está um exemplo de como você pode fazer essa alteração:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<IdentityUserLogin<string>>(entity =>
    {
        entity.Property(e => e.LoginProvider).HasMaxLength(255);
        entity.Property(e => e.ProviderKey).HasMaxLength(255);
    });

    // Restante do código...
}

Lembre-se de substituir IdentityUserLogin<string> pelo nome da sua classe de login personalizada, se você estiver usando uma.

Espero que essa solução ajude a resolver o problema. Se você tiver mais alguma dúvida, é só me dizer! Espero ter ajudado e bons estudos!