System.InvalidCastException 发生在 EntityFramework.Core.dll
System.InvalidCastException occurred in EntityFramework.Core.dll
下面是Application Entity
:
public class Application
{
[Key]
[Column(TypeName = "integer")]
public int ApplicationID { get; set; }
[Required]
[Column(TypeName = "nvarchar(50)")]
public string ApplicationName { get; set; }
[Column(TypeName = "nvarchar(150)")]
public string Description { get; set; }
[Required]
[ForeignKey("mTechnology")]
[Column(TypeName = "integer")]
public int TechnologyID { get; set; }
[Required]
[Column(TypeName = "int")]
public string CreatedBy { get; set; }
[Required]
[Column(TypeName = "datetime")]
public DateTime CreatedDate { get; set; }
public virtual mTechnology Technology { get; set; }
}
在执行以下 LINQ 查询时,我在 foreach loop
中的 run-time
处收到错误
List<int> technologyList = new List<int>();
var query = from a in _mApplicationDbContext.Applications
group a.Technology by a.TechnologyID into g
select new
{
TechnologyID = g.Key
};
foreach (var item in query)
{
technologyList.Add(item.TechnologyID);
}
错误信息如下:
An exception of type 'System.InvalidCastException' occurred in
EntityFramework.Core.dll but was not handled in user code
Additional information: Unable to cast object of type 'System.Int32'
to type 'System.String'.
LINQ 查询是否错误或任何其他错误?
从表面上看,你在模特方面犯了错误。
当您将外键 TechnologyID 定义为整数时,您试图使用无效的数据类型。在不知道您使用的是什么类型的数据库的情况下,我假设您使用的是某种类型的 sql 服务器,在这种情况下数据类型 "integer" 不存在。
波纹管应该可以解决问题:
public class Application
{
[Key]
[Column]
public int ApplicationID { get; set; }
[Required]
[Column(TypeName = "nvarchar(50)")]
public string ApplicationName { get; set; }
[Column(TypeName = "nvarchar(150)")]
public string Description { get; set; }
[Required]
[ForeignKey("mTechnology")]
[Column]
public int TechnologyID { get; set; }
[Required]
[Column(TypeName = "int")]
public string CreatedBy { get; set; }
[Required]
[Column]
public DateTime CreatedDate { get; set; }
public virtual mTechnology Technology { get; set; }
}
附带说明一下,您并不总是需要为所有属性指定类型名称。如果您有一个 int 属性 并将其映射到一个 int 列,您可以只使用 [Column] 属性,EF 将使用正确的 int 类型。当 sql 服务器中没有 long 时,当您尝试在模型中使用 long 时,指定类型名称更为重要,因此您将使用 TypeName 属性 as [Column(TypeName = "bigint")]
下面是Application Entity
:
public class Application
{
[Key]
[Column(TypeName = "integer")]
public int ApplicationID { get; set; }
[Required]
[Column(TypeName = "nvarchar(50)")]
public string ApplicationName { get; set; }
[Column(TypeName = "nvarchar(150)")]
public string Description { get; set; }
[Required]
[ForeignKey("mTechnology")]
[Column(TypeName = "integer")]
public int TechnologyID { get; set; }
[Required]
[Column(TypeName = "int")]
public string CreatedBy { get; set; }
[Required]
[Column(TypeName = "datetime")]
public DateTime CreatedDate { get; set; }
public virtual mTechnology Technology { get; set; }
}
在执行以下 LINQ 查询时,我在 foreach loop
run-time
处收到错误
List<int> technologyList = new List<int>();
var query = from a in _mApplicationDbContext.Applications
group a.Technology by a.TechnologyID into g
select new
{
TechnologyID = g.Key
};
foreach (var item in query)
{
technologyList.Add(item.TechnologyID);
}
错误信息如下:
An exception of type 'System.InvalidCastException' occurred in EntityFramework.Core.dll but was not handled in user code
Additional information: Unable to cast object of type 'System.Int32' to type 'System.String'.
LINQ 查询是否错误或任何其他错误?
从表面上看,你在模特方面犯了错误。
当您将外键 TechnologyID 定义为整数时,您试图使用无效的数据类型。在不知道您使用的是什么类型的数据库的情况下,我假设您使用的是某种类型的 sql 服务器,在这种情况下数据类型 "integer" 不存在。
波纹管应该可以解决问题:
public class Application
{
[Key]
[Column]
public int ApplicationID { get; set; }
[Required]
[Column(TypeName = "nvarchar(50)")]
public string ApplicationName { get; set; }
[Column(TypeName = "nvarchar(150)")]
public string Description { get; set; }
[Required]
[ForeignKey("mTechnology")]
[Column]
public int TechnologyID { get; set; }
[Required]
[Column(TypeName = "int")]
public string CreatedBy { get; set; }
[Required]
[Column]
public DateTime CreatedDate { get; set; }
public virtual mTechnology Technology { get; set; }
}
附带说明一下,您并不总是需要为所有属性指定类型名称。如果您有一个 int 属性 并将其映射到一个 int 列,您可以只使用 [Column] 属性,EF 将使用正确的 int 类型。当 sql 服务器中没有 long 时,当您尝试在模型中使用 long 时,指定类型名称更为重要,因此您将使用 TypeName 属性 as [Column(TypeName = "bigint")]