转换为值类型 'System.Int32' 失败,因为具体化值为空。但它适用于类似的代码
The cast to value type 'System.Int32' failed because the materialized value is null. But it works on similar code
在我的控制器中,我正在数据库中搜索关联 table 中的特定记录。
public ActionResult MyAction(int picId, int myOtherId)
{
var itemToEdit =
db.AssocationTable.Single(
x => x.PropertyId == picId && x.MyForeignKey == myOtherId);
var myRecord = db.TestTableOne.Find(itemToEdit.PropertyId);
var lstOfIntsINeed =
db.AssocationTable.Where(
x => x.PropertyId == picId && x.MyForeignKey != myOtherId)
.Select(x => x.MyForeignKey.Value)
.ToList();
// the list above, if nothing is found, will give me an empty list where Count == 0
}
现在我正在执行 完全相同的 代码,但用于不同的 属性 我收到:
The cast to value type 'System.Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
这是我的代码:
public ActionResult MyOtherAction(int picId, int myOtherId)
{
var itemToEdit =
db.AssociationTable.Single(
x => x.PropertyId == picId && x.MyOtherForeignKey == myOtherId);
var myMainRecord = db.TestTableOne.Find(itemToEdit.PropertyId);
var lstOfIntsINeed =
db.AssociationTable.Where(
x => x.PropertyId == picId && x.MyOtherForeignKey != myOtherId)
.Select(x => x.MyOtherForeignKey.Value)
.ToList();
}
在我的数据库和领域模型中 Class 我的协会 Table MyForeignKey
和 MyOtherForeignKey
都可以为空:
[ForeignKey("TestTableTwo")]
public int? MyForeignKey { get; set; }
[ForeignKey("TestTableThree")]
public int? MyOtherForeignKey { get; set; }
所以,我的问题是.. 为什么第一个 ActionResult 是第一个接收到 0 的计数(这是我所期望的),而第二个 ActionResult 接收到异常错误?
我认为问题在于您在 nullable int
上调用 .Value
而未首先检查 null
。最终这意味着您正在查询中间执行缩小隐式转换。
如果代码在 .NET 中为 运行,并且 x.MyOtherForeignKey
为 null,它将引发 Nullable object must have a value
异常。在这种情况下,代码被转换为 Linq-to-Sql 表达式和 运行 in EntityFramework。我认为它在你的第二个查询中找到了一个 null x.MyOtherForeignKey
并给出了这个异常。
根据您希望代码在遇到 null
时执行的操作,您可以更改它以过滤掉 nulls
:
var lstOfIntsINeed =
db.AssociationTable.Where(
x => x.PropertyId == picId && x.MyOtherForeignKey != myOtherId && x.MyOtherForeignKey != null)
.Select(x => x.MyOtherForeignKey.Value)
.ToList();
... 或用默认值替换 null。
我认为你的第一个查询也存在同样的问题,它在你的测试中没有失败的唯一原因是查询 return 没有条目(或者更具体地说,没有条目null
MyForeignKey
),所以它没有失败。
鉴于您已将 MyForeignKey
定义为可为空,您必须考虑它为空的情况,因此在该查询中也修复它是明智的。
您可以通过将 .Where()
子句更改为故意 return 具有空值的 AssociationTable 条目来测试是否属于这种情况。
希望对您有所帮助
在我的控制器中,我正在数据库中搜索关联 table 中的特定记录。
public ActionResult MyAction(int picId, int myOtherId)
{
var itemToEdit =
db.AssocationTable.Single(
x => x.PropertyId == picId && x.MyForeignKey == myOtherId);
var myRecord = db.TestTableOne.Find(itemToEdit.PropertyId);
var lstOfIntsINeed =
db.AssocationTable.Where(
x => x.PropertyId == picId && x.MyForeignKey != myOtherId)
.Select(x => x.MyForeignKey.Value)
.ToList();
// the list above, if nothing is found, will give me an empty list where Count == 0
}
现在我正在执行 完全相同的 代码,但用于不同的 属性 我收到:
The cast to value type 'System.Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
这是我的代码:
public ActionResult MyOtherAction(int picId, int myOtherId)
{
var itemToEdit =
db.AssociationTable.Single(
x => x.PropertyId == picId && x.MyOtherForeignKey == myOtherId);
var myMainRecord = db.TestTableOne.Find(itemToEdit.PropertyId);
var lstOfIntsINeed =
db.AssociationTable.Where(
x => x.PropertyId == picId && x.MyOtherForeignKey != myOtherId)
.Select(x => x.MyOtherForeignKey.Value)
.ToList();
}
在我的数据库和领域模型中 Class 我的协会 Table MyForeignKey
和 MyOtherForeignKey
都可以为空:
[ForeignKey("TestTableTwo")]
public int? MyForeignKey { get; set; }
[ForeignKey("TestTableThree")]
public int? MyOtherForeignKey { get; set; }
所以,我的问题是.. 为什么第一个 ActionResult 是第一个接收到 0 的计数(这是我所期望的),而第二个 ActionResult 接收到异常错误?
我认为问题在于您在 nullable int
上调用 .Value
而未首先检查 null
。最终这意味着您正在查询中间执行缩小隐式转换。
如果代码在 .NET 中为 运行,并且 x.MyOtherForeignKey
为 null,它将引发 Nullable object must have a value
异常。在这种情况下,代码被转换为 Linq-to-Sql 表达式和 运行 in EntityFramework。我认为它在你的第二个查询中找到了一个 null x.MyOtherForeignKey
并给出了这个异常。
根据您希望代码在遇到 null
时执行的操作,您可以更改它以过滤掉 nulls
:
var lstOfIntsINeed =
db.AssociationTable.Where(
x => x.PropertyId == picId && x.MyOtherForeignKey != myOtherId && x.MyOtherForeignKey != null)
.Select(x => x.MyOtherForeignKey.Value)
.ToList();
... 或用默认值替换 null。
我认为你的第一个查询也存在同样的问题,它在你的测试中没有失败的唯一原因是查询 return 没有条目(或者更具体地说,没有条目null
MyForeignKey
),所以它没有失败。
鉴于您已将 MyForeignKey
定义为可为空,您必须考虑它为空的情况,因此在该查询中也修复它是明智的。
您可以通过将 .Where()
子句更改为故意 return 具有空值的 AssociationTable 条目来测试是否属于这种情况。
希望对您有所帮助