gpt4 book ai didi

c# - Entity Framework 。外键引用不存在的项目

转载 作者:行者123 更新时间:2023-11-29 02:47:15 25 4
gpt4 key购买 nike

我在导出到 Excel 文件时收到空对象引用异常错误。问题是一条记录在数据库中有外部,而在其他表中没有项目。我无法更改数据库。

if (item.ShipperId != null)
{
str.Append("<td><font face=Arial Narrow size=" + "14px" + ">" + item.Shipper.ShipperName + "" + "</font></td>");
}
else
{
str.Append("<td><font face=Arial Narrow size=" + "14px" + ">" + "" + "</font></td>");
}
}

ShipperId 是外键,在这种情况下无效。它给出异常(exception)

item.Shipper.ShipperName

我试过检查它是否为空

if (item.Shipper.ShipperName != null)

但它也给出了这个空检查的异常

最佳答案

引用 Shipper 为空。而不是 ShipperName

请检查if (item.Shipper != null)

关于c# - Entity Framework 。外键引用不存在的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40394851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com