[英]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。
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.