Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

368
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!