3

I am implementing Mysql.EntityFramework.Core with NET CORE 3.1. When fetching data it gives the error "Unable to cast object of type 'System.String' to type 'System.Byte []'." Example in:

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;
    }

In my configurationDALContext say:

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

Model Rol:

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; }

}

And the table "Rol":

enter image description here


Exception:

enter image description here

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

Can someone help solve this?

jps
  • 20,041
  • 15
  • 75
  • 79
JUAN VERA
  • 51
  • 1
  • 5

2 Answers2

2

Finally solved it by installing Nuget "Pomelo.EntityFrameworkCore.MySql" and uninstalling Mysql.Core. This automatically parses it from Char to Guid.

enter link to solution in other post

JUAN VERA
  • 51
  • 1
  • 5
1

The Id column in the database is a string. This is what is returned from the query. But the Id property in the C# side is a Guid. C# Guid values are binary; you have a mismatch there. You need to have Guid.Parse() somewhere, or change the C# Id property to string.

Also, guids are fixed-length, so if that's really what you're doing, varchar(64) seems kind of odd. Depending on how they are formatted, you want one of char(32), char(36), char(38), or char(68).

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Changed the table's ID type to "Char (36)", but the Mysql.Core was still not parsing. In the "mapping" I couldn't do it, but investigating I found that with "Pomelo.EntityFrameworkCore.MySql" it parses it automatically. I tried and it worked. Above all, you have helped me a lot to understand it. Thank you so much for help me! – JUAN VERA Jul 08 '20 at 01:40