Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

377
Visualizações
No se puede convertir el objeto de tipo 'System.String' para escribir 'System.Byte[]' Error MYSQL NET CORE 3.1

Estoy implementando Mysql.EntityFramework.Core con NET CORE 3.1. Al obtener datos, aparece el error "No se puede convertir el objeto de tipo 'System.String' para escribir 'System.Byte []'". Ejemplo en:

 public new async Task<IList<Rol>> GetAllAsync(Expression<Func<Rol, bool>> condition) { var roles = await _dbContext.Rol // <=== HERE Line 26 RolDataStore .Where(condition).ToListAsync(); return roles; }

En mi configuraciónDALContext decir:

 public static void ConfigureDALContext(this IServiceCollection services, string dbConnection) { services.AddDbContext<MyDbContext>(options => { options.UseMySQL(dbConnection); }); }

Rol del modelo:

 public class Rol : IEntity, ISoftDeleteEntity { private Guid _id; public Guid Id { get { return _id == Guid.Empty ? Guid.NewGuid() : _id; } set { _id = value; } } public string Nombre { get; set; } public string Descripcion { get; set; } public virtual ICollection<Usuario> Usuarios { get; set; } public DateTime? FechaBaja { get; set; } }

Y la mesa "Rol":

ingrese la descripción de la imagen aquí


Excepción:

ingrese la descripción de la imagen aquí

 System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Byte[]'. at System.Data.Common.DbDataReader.GetFieldValue[T](Int32 ordinal) at MySql.Data.MySqlClient.MySqlDataReader.GetFieldValue[T](Int32 ordinal) at lambda_method(Closure , QueryContext , DbDataReader , ResultContext , Int32[] , ResultCoordinator ) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync() Excepción producida: 'System.InvalidCastException' en System.Private.CoreLib.dll System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Byte[]'. at System.Data.Common.DbDataReader.GetFieldValue[T](Int32 ordinal) at MySql.Data.MySqlClient.MySqlDataReader.GetFieldValue[T](Int32 ordinal) at lambda_method(Closure , QueryContext , DbDataReader , ResultContext , Int32[] , ResultCoordinator ) at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.AsyncEnumerator.MoveNextAsync() at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken) at GPS.DAL.DataStores.RolDataStore.GetAllAsync(Expression`1 condition) in D:\PROYECTOS\GPSimracing\gpsimracing\api\GPS.DAL\DataStores\RolDataStore.cs:line 26

¿Alguien puede ayudar a resolver esto?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Finalmente lo resolvió instalando Nuget "Pomelo.EntityFrameworkCore.MySql" y desinstalando Mysql.Core. Esto lo analiza automáticamente de Char a Guid.

ingrese el enlace a la solución en otra publicación

over 4 years ago · Santiago Trujillo Relatório

0

La columna Id en la base de datos es una cadena. Esto es lo que se devuelve de la consulta. Pero la propiedad Id en el lado de C# es un Guid . Los valores de C# Guid son binarios; ahí tienes un desajuste. Debe tener Guid.Parse() en alguna parte, o cambiar la propiedad C# Id a cadena.

Además, las guías tienen una longitud fija, por lo que si eso es realmente lo que estás haciendo, varchar(64) parece un poco extraño. Dependiendo de cómo estén formateados , querrá uno de char(32), char(36), char(38) o char(68).

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda