EF4.1 预加载的链接对象返回 null
谁能解释为什么返回 Company 但 Company.CompanyServices 为空(即使我在测试中创建了一个)?
public List<Company> GetContactCompanies(int contactId)
{
var query = (
from directorCompany in ctx.CompanyDirectors
.Where(d => d.ContactAddress.Contact.Id == contactId)
.Include(d => d.Company.CompanyServices)
select directorCompany.Company
).OrderBy(c => c.CompanyName).Distinct();
return query.ToList();
}
请注意,将 Include 替换为 .Include("Company.CompanyServices")
无效
Can anyone explain why a Company is returned but Company.CompanyServices is null (even though I've created one in the test)?
public List<Company> GetContactCompanies(int contactId)
{
var query = (
from directorCompany in ctx.CompanyDirectors
.Where(d => d.ContactAddress.Contact.Id == contactId)
.Include(d => d.Company.CompanyServices)
select directorCompany.Company
).OrderBy(c => c.CompanyName).Distinct();
return query.ToList();
}
Note substituting the Include for .Include("Company.CompanyServices")
has no effect
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Company.CompanyServices 属性是否标记为虚拟? 查看 ScottGu 的关于实体框架的博客,他在其中创建具有一对多关系的 POCO 类,并将集合属性标记为虚拟。
当我第一次开始使用 EF 4 时,这让我困惑了很长一段时间。
显然我看不到你的实体类,所以这可能是一个有争议的问题!
Is the Company.CompanyServices property marked as virtual? Check out ScottGu's blog on entity framework, where he creates POCO classes with one to many relationships he marks the collection properties as virtual.
When I first started using EF 4, that had me stumped for quite a while.
Obviously I can't see your entity classes so this may be a moot point!
找到了一个并不完全直观的答案,但它有效,这是最重要的。
很高兴看到一个在原始查询上播放的...
Found an answer that's not wholly inuitive, but it works which is the main thing.
Happy to see one that plays on the original query...