如何判断哪个 table 列导致无效转换异常?
How to tell which table column is causing an invalid cast exception?
使用 EF Core 3.1,我尝试使用以下代码从数据库(SQL 服务器)获取 table:
query = dbContext.Set<Entity>().ToList();
该代码适用于其他 table,但在特定 table 上失败,但出现以下异常:
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)
在 try-catch 中使用断点没有帮助,因为内部异常为空。我如何判断哪个 table 列导致转换异常?
型号代码:
[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; }
}
我附上了 SSMS 数据库列的屏幕截图。
您正在将 属性 映射为 int
,这应该是 byte
,并且只有一个 属性 映射为 int
,即 Serial
,所以应该是那个。
问题是
public ObjectStatus ObjectStatus { get; set; }
& 必须按如下方式创建枚举:
public enum ObjectStatus : byte
{
//List of values
}
问题是 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;
}
}
使用 EF Core 3.1,我尝试使用以下代码从数据库(SQL 服务器)获取 table:
query = dbContext.Set<Entity>().ToList();
该代码适用于其他 table,但在特定 table 上失败,但出现以下异常:
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)
在 try-catch 中使用断点没有帮助,因为内部异常为空。我如何判断哪个 table 列导致转换异常?
型号代码:
[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; }
}
我附上了 SSMS 数据库列的屏幕截图。
您正在将 属性 映射为 int
,这应该是 byte
,并且只有一个 属性 映射为 int
,即 Serial
,所以应该是那个。
问题是
public ObjectStatus ObjectStatus { get; set; }
& 必须按如下方式创建枚举:
public enum ObjectStatus : byte
{
//List of values
}
问题是 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;
}
}