C# 无法强制转换类型为';System.String';输入';字节[]和#x27;错误MYSQL NET CORE 3.1
我正在用NET Core 3.1实现Mysql.EntityFramework.Core。 获取数据时,会出现错误“无法将类型为“System.String”的对象强制转换为类型为“System.Byte[]” 例如:C# 无法强制转换类型为';System.String';输入';字节[]和#x27;错误MYSQL NET CORE 3.1,c#,mysql,.net-core,core,C#,Mysql,.net Core,Core,我正在用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
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;
}
数据库中的
Id
列是一个字符串。这是查询返回的内容。但是C端的Id
属性是Guid
。C#Guid值是二进制的;你有一个不匹配的地方。您需要在某个地方设置Guid.Parse()
,或者将C#Id
属性更改为string
而且,guid是固定长度的,所以如果你真的这么做,
varchar(64)
似乎有点奇怪。根据需要,您需要char(32)、char(36)、char(38)或char(68)中的一个。最后通过安装Nuget“pomero.EntityFrameworkCore.MySql”并卸载MySql.Core解决了这个问题。
这会自动将其从Char解析为Guid
您也应该显示您的
Rol
类型。我认为数据库中有一个文本列,实体中有一个字节数组!好的,还显示对exception.addexception的回溯,将表的ID类型更改为“Char(36)”,但是Mysql.Core仍然没有解析。在“映射”中,我无法做到这一点,但通过调查,我发现使用“Pomelo.EntityFrameworkCore.MySql”可以自动解析它。我试过了,成功了。最重要的是,你帮助我了解了很多。非常感谢你对我的帮助!您安装了哪个版本的Polemo,目前安装在3.2.4上,但这并没有解决问题
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; }
}
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