简体   繁体   English

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

[英]Unable to cast object of type 'System.String' to type 'System.Byte[]' Error MYSQL NET CORE 3.1

I am implementing Mysql.EntityFramework.Core with NET CORE 3.1.我正在使用 NET CORE 3.1 实现 Mysql.EntityFramework.Core。 When fetching data it gives the error "Unable to cast object of type 'System.String' to type 'System.Byte []'."获取数据时,它会给出错误“无法将'System.String'类型的object转换为'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:在我的配置 DALContext 中说:

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

Can someone help solve this?有人可以帮助解决这个问题吗?

Thanks very much!!非常感谢!!


edit.编辑。 Model Rol: Model 角色:

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":和表“Rol”:

在此处输入图像描述


Exception:例外:

在此处输入图像描述

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

Finally solved it by installing Nuget "Pomelo.EntityFrameworkCore.MySql" and uninstalling Mysql.Core.最后通过安装 Nuget "Pomelo.EntityFrameworkCore.MySql" 并卸载 Mysql.Core 解决了这个问题。 This automatically parses it from Char to Guid.这会自动将其从 Char 解析为 Guid。

enter link to solution in other post在其他帖子中输入解决方案的链接

The Id column in the database is a string.数据库中的Id列是一个字符串。 This is what is returned from the query.这是从查询返回的内容。 But the Id property in the C# side is a Guid .但是 C# 端的Id属性是Guid C# Guid values are binary; C# Guid 值是二进制的; you have a mismatch there.你那里不匹配。 You need to have Guid.Parse() somewhere, or change the C# Id property to string.您需要在某处有Guid.Parse() ,或者将 C# Id属性更改为字符串。

Also, guids are fixed-length, so if that's really what you're doing, varchar(64) seems kind of odd.此外,guid 是固定长度的,所以如果这真的是你正在做的事情, varchar(64)似乎有点奇怪。 Depending on how they are formatted , you want one of char(32), char(36), char(38), or char(68).根据它们的格式,您需要 char(32)、char(36)、char(38) 或 char(68) 之一。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”的对象。 发布后发生错误 - Unable to cast object of type 'System.String' to type 'System.Byte[]'. Error after publish Linq-收到错误“无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”。” - Linq - Receiving error “Unable to cast object of type 'System.String' to type 'System.Byte[]'.” 无法将类型为“ System.Byte”的对象转换为类型为“ System.String”的对象 - Unable to cast object of type 'System.Byte' to type 'System.String' (已解决)无法将“system.byte”类型的 object 转换为“system.string”类型(组合框填充 PictureBox) - (Solved) unable to cast object of type 'system.byte ' to type 'system.string' (combobox populate PictureBox) 无法将类型为“ System.Byte []”的对象转换为类型为“ System.String []”的对象 - Unable to cast object of type 'System.Byte[]' to type 'System.String[]' 无法将“System.Byte”类型的 object 转换为“System.String”类型。 - Unable to cast object of type 'System.Byte' to type 'System.String'.' 从数据库读取数据导致无法将类型为“ System.Byte”的对象转换为类型为“ System.String”的错误 - Reading data from database gives Unable to cast object of type 'System.Byte' to type 'System.String' error 无法将System.string强制转换为System.Byte [] - Unable to cast System.string to System.Byte[] 无法将“System.DBNull”类型的对象转换为 Scaffold-DbContext .Net Core 中的“System.String”类型 - Unable to cast object of type 'System.DBNull' to type 'System.String' in Scaffold-DbContext .Net Core 错误:System.InvalidCastException:无法将“System.Byte”类型的对象转换为“System.Int32”类型 - Error: System.InvalidCastException: Unable to cast object of type 'System.Byte' to type 'System.Int32'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM