【问题标题】:Unable to cast object of type 'System.String' to type 'System.Byte[]' Error MYSQL NET CORE 3.1无法将类型“System.String”的对象转换为类型“System.Byte []”错误 MYSQL NET CORE 3.1
【发布时间】:2020-10-28 06:01:59
【问题描述】:

我正在使用 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;
    }

在我的配置DALContext中说:

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”:


例外:

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

有人可以帮忙解决这个问题吗?

【问题讨论】:

    标签: c# mysql .net-core


    【解决方案1】:

    最后通过安装 Nuget "Pomelo.EntityFrameworkCore.MySql" 并卸载 Mysql.Core 解决了。 这会自动将其从 Char 解析为 Guid。

    enter link to solution in other post

    【讨论】:

    • 你安装的是什么版本的 Polemo,目前是 3.2.4,这并没有解决问题
    【解决方案2】:

    数据库中的Id 列是一个字符串。这是从查询返回的内容。但是 C# 端的Id 属性是Guid。 C# Guid 值是二进制的;你那里不匹配。您需要在某处拥有Guid.Parse(),或者将C# Id 属性更改为字符串。

    另外,guid 是固定长度的,所以如果你真的这样做,varchar(64) 似乎有点奇怪。根据how they are formatted,您需要char(32)、char(36)、char(38) 或char(68) 之一。

    【讨论】:

    • 将表的 ID 类型更改为“Char (36)”,但 Mysql.Core 仍未解析。在“映射”中我做不到,但调查我发现使用“Pomelo.EntityFrameworkCore.MySql”它会自动解析它。我试过了,它奏效了。最重要的是,你帮助我理解了它。非常感谢您对我的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多