[英]Unable to cast object of type 'System.String' to type 'System.Byte[]'. Error after publish
[英]Unable to cast object of type 'System.String' to type 'System.Byte[]' Error MYSQL NET CORE 3.1
我正在使用 NET CORE 3.1 實現 Mysql.EntityFramework.Core。 獲取數據時,它會給出錯誤“無法將'System.String'類型的object轉換為'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);
});
}
有人可以幫助解決這個問題嗎?
非常感謝!!
編輯。 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; }
}
和表“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
最后通過安裝 Nuget "Pomelo.EntityFrameworkCore.MySql" 並卸載 Mysql.Core 解決了這個問題。 這會自動將其從 Char 解析為 Guid。
數據庫中的Id
列是一個字符串。 這是從查詢返回的內容。 但是 C# 端的Id
屬性是Guid
。 C# Guid 值是二進制的; 你那里不匹配。 您需要在某處有Guid.Parse()
,或者將 C# Id
屬性更改為字符串。
此外,guid 是固定長度的,所以如果這真的是你正在做的事情, varchar(64)
似乎有點奇怪。 根據它們的格式,您需要 char(32)、char(36)、char(38) 或 char(68) 之一。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.