无法将类型为“System.String”的对象转换为类型“System.Byte[]”错误 MYSQL NET CORE 3.1

3

我正在使用 NET CORE 3.1 实现 Mysql.EntityFramework.Core。在获取数据时出现错误:"无法将类型为 'System.String' 的对象转换为类型 'System.Byte[]'"。

示例:

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

在我的配置DAL上下文中说:
public static void ConfigureDALContext(this IServiceCollection services, string dbConnection)
    {
        services.AddDbContext<MyDbContext>(options =>
        {
            options.UseMySQL(dbConnection);
        });
        
    }

模型角色:

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

}

以及“Rol”表:

enter image description here


异常:

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

有人能帮忙解决吗?

2个回答

2

你安装了哪个版本的Polemo?当前版本是3.2.4,但这并没有解决问题。 - mjwrazor

1

数据库中的Id列是一个字符串,这是从查询中返回的。但是C#端的Id属性是一个Guid。C# Guid值是二进制的,你需要在某个地方使用Guid.Parse(),或者将C#的Id属性更改为字符串。

此外,guid是固定长度的,所以如果你真的在做这件事,varchar(64)似乎有点奇怪。根据它们的格式如何格式化, 你想要其中之一:char(32), char(36), char(38)或char(68)。


将表的ID类型更改为“Char(36)”,但Mysql.Core仍无法解析。在“映射”中我无法做到,但是调查后发现使用“Pomelo.EntityFrameworkCore.MySql”可以自动解析它。我尝试了一下,它确实有效。最重要的是,你帮助我很多地理解了它。非常感谢你的帮助! - JUAN VERA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接