EF4.1 预加载的链接对象返回 null

发布于 2024-11-07 09:21:19 字数 663 浏览 0 评论 0原文

谁能解释为什么返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如此安好 2024-11-14 09:21:19

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!

情栀口红 2024-11-14 09:21:19

找到了一个并不完全直观的答案,但它有效,这是最重要的。

很高兴看到一个在原始查询上播放的...

var query = (
                    from directorCompany in ctx.CompanyDirectors
                      .Where(d => d.ContactAddress.Contact.Id == contactId)
                    select directorCompany.Company
                  ).OrderBy(c => c.CompanyName).Distinct();
      return query.Include(c => c.CompanyServices).ToList();

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...

var query = (
                    from directorCompany in ctx.CompanyDirectors
                      .Where(d => d.ContactAddress.Contact.Id == contactId)
                    select directorCompany.Company
                  ).OrderBy(c => c.CompanyName).Distinct();
      return query.Include(c => c.CompanyServices).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文