使用 EF 4.1 ObjectContext 在 LINQ 查询中提前加载实体

发布于 2025-01-07 06:54:01 字数 416 浏览 2 评论 0原文

我的 EDMX 中有一堆实体,我将 LazyLoading 设置为 false。 据我了解,我现在必须在每个查询中显式加载相关实体。 然而,我发现的大多数引用都指向 DbContext 而不是 ObjectContext

代码中的急切加载方式似乎存在很大差异。 就我而言,客户和地址之间存在 1:1 关系。

获取时如何正确加载地址,例如客户#1488?

from c in context.Customers
where c.Id = 1488
select c;

然后我希望能够使用 c.Address.Streetc.Address.City 等。

如何加载地址?

I have a bunch of entities in my EDMX and I set LazyLoading to false.
As I understand I now have to explicitly load realted entities with every query.
however most references I found point to DbContext and not ObjectContext.

And there seems to be great differences as how eager loading is done in code.
In my case I have Customers and Addresses in a 1:1 relationship.

How do I correctly load the address when fetching, e.g. customer #1488?

from c in context.Customers
where c.Id = 1488
select c;

Then I want to be able to use c.Address.Street, c.Address.City etc.

How do I load the Address(es)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

送君千里 2025-01-14 06:54:01

其他人可能会给您有关 DbContext 的更准确答案。我通常使用 ObjectContext ,其中以下代码将完成这项工作:

from c in context.Customers.Include("Addresses")
where c.Id = 1488
select c;

Somebody else may give you a much more precise answer about DbContext. I generally work with ObjectContext where this code will do the job:

from c in context.Customers.Include("Addresses")
where c.Id = 1488
select c;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文