如何判断哪个表列导致无效转换异常?

bae*_*ein 6 c# ef-core-3.1

使用 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>();
}   

[Key]
[Column("ID")]
public Guid ID { get; set; }
public int Serial { get; set; }
[Column(TypeName = "datetime")]
public DateTime? DateCreated { get; set; }
[Column(TypeName = "datetime")]
public DateTime? DateModified { get; set; }
public ObjectStatus ObjectStatus { get; set; }
[Required]
public byte[] Timestamp { get; set; }
public PluginType Type { get; set; }
[StringLength(64)]
public string Name { get; set; }
[StringLength(512)]
public string ProcessorAssembly { get; set; }
[StringLength(512)]
public string ProcessorClass { get; set; }
[StringLength(512)]
public string AdminAssembly { get; set; }
[StringLength(512)]
public string AdminClass { get; set; }
[StringLength(32)]
public string ConfigName { get; set; }
public ProfileType SubType { get; set; }
[StringLength(256)]
public string ContainerType { get; set; }
[StringLength(256)]
public string SupportedFeatures { get; set; }

[InverseProperty(nameof(SetProfile.SetAPlugin))]
public virtual ICollection<SetProfile> SetAPlugins { get; set; }
[InverseProperty(nameof(SetProfile.SetBplugin))]
public virtual ICollection<SetProfile> SetBPlugins { get; set; }
[InverseProperty(nameof(SetProfile.SetCPlugin))]
public virtual ICollection<SetProfile> SetCPlugins { get; set; }
[InverseProperty(nameof(SetProfile.SetDPlugin))]
public virtual ICollection<SetProfile> SetDPlugins { get; set; }
[InverseProperty(nameof(SetProfile.SetEPlugin))]
public virtual ICollection<SetProfile> SetEPlugins { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我附上了 SSMS 的数据库列的屏幕截图。 在此输入图像描述

bae*_*ein 1

问题在于 SQL 类型是tinyint,它是代码中的字节,无法隐式转换为枚举。我使用以下代码来解决这个问题:

        [Column("ObjectStatus")]
        public Byte objectStatus { get; set; }

        [NotMapped]
        public ObjectStatus ObjectStatus
        {
            get
            {
                if (objectStatus > 0)               
                    return (ObjectStatus)objectStatus;            
                else
                    return ObjectStatus.Active;
            }
            set
            {
                objectStatus = (Byte)value;
            }
        }
Run Code Online (Sandbox Code Playgroud)