以下是我的代码:
Entity.Invoice inStoreInvoice = invoiceRepository.Find(x => x.AccountId == accountId && x.CompanyId == companyId && x.Id == invoiceNum);
if (inStoreInvoice.CustomerMaster.SomeId == null)
{
// External code which updates the CustomerMaster with SomeId value.
// .....
// .....
// Now I want to reload inStoreInvoice with updated value of CustomerMaster.SomeId
inStoreInvoice = invoiceRepository.Find(x => x.AccountId == accountId && x.CompanyId == companyId && x.Id == invoiceNum);
}
string value = inStoreInvoice.CustomerMaster.SomeId; // This gives me null..!!
代码似乎不言自明(如果您需要我详细阐述任何一点,请告诉我)。 invoiceRepository.Find()在内部调用dbset.FirstOrDefault()..即从列表中获取与查询表达式匹配的第一个实体。 CustomerMaster是Invoice实体的导航属性
我的问题是,为什么声明,
string value = inStoreInvoice.CustomerMaster.SomeId; // This gives me null..!!
给出一个空值?!我尝试了以下选项,但没有工作,相同的结果(实体是我传递的inStoreInvoice对象)。
1. context.Entry(entity).State = EntityState.Detached;
2. context.Entry(entity).Reload();
3. ((IObjectContextAdapter)context).ObjectContext.Detach(entity);
4. ((IObjectContextAdapter)context).ObjectContext.Refresh(RefreshMode.StoreWins, entity);
思想?
谢谢!