使用 EF Core 3.1,我尝试使用以下代码从数据库(SQL Server)获取表:
query = dbContext.Set<Entity>().ToList();
Run Code Online (Sandbox Code Playgroud)
该代码适用于其他表,但在特定表上失败,但出现以下异常:
System.InvalidCastException: Unable to cast object of type 'System.Byte' to type 'System.Int32'.
at Microsoft.Data.SqlClient.SqlBuffer.get_Int32()
at Microsoft.Data.SqlClient.SqlDataReader.GetInt32(Int32 i)
at lambda_method(Closure , QueryContext , DbDataReader , ResultContext , Int32[] , ResultCoordinator )
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
Run Code Online (Sandbox Code Playgroud)
在 try-catch 中使用断点没有帮助,因为内部异常为空。如何判断哪个表列导致了强制转换异常?
型号代码:
[Table("Plugin")]
public partial class Plugin
{
public Plugin()
{
SetAPlugins = new HashSet<SetProfile>();
SetBplugins = new HashSet<SetProfile>();
SetBPlugins = new HashSet<SetProfile>();
SetBPlugins = new HashSet<SetProfile>();
SetBPlugins = new HashSet<SetProfile>();
} …Run Code Online (Sandbox Code Playgroud)