【发布时间】:2011-05-03 14:41:13
【问题描述】:
一个客户有多个 ReservationRequest,一个 ReservationRequest 只有一个客户。
假设我这样检索我的 ReservationRequest
var c = dataContext.ReservationRequestSet.FirstOrDefault(i => i.id == RequestId);
我得到我的 ReservationRequest 没有问题,但是当我做这样的事情时。
if (c != null)
{
int id = c.Customers.id;
我得到一个
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 75: if (c != null)
Line 76: {
Line 77: int id = c.Customers.id;
我在 EF 方面的经验很少,但是这种类型的东西可以在 nHibernate 中正常工作,我是否缺少 EF 中的某个设置?
谢谢 吉姆
【问题讨论】:
-
您使用的是哪个版本的实体框架?最新的支持延迟加载,正如您所期望的那样,否则您必须编写一些额外的代码。
-
有没有简单的检查方法?
-
我建议您更改“客户”中 ReservationRequestSet“客户”的导航属性。它更红。
标签: asp.net linq entity-framework entity nullreferenceexception