Linq-To-Entity 包括

发布于 2024-08-08 08:02:59 字数 629 浏览 4 评论 0原文

我目前正在学习更多有关 Linq-To-Entities 的知识 - 特别是目前有关急切加载和延迟加载的知识。

proxy.User.Include("Role").First(u => u.UserId == userId)

这应该加载用户以及用户拥有的任何角色。我有一个问题,但我也有一个问题。这只是一个为了解 L2E 而创建的简单模型,

我的印象是,这是为了使事物强类型化而设计的 - 那么为什么我必须写“角色”呢?看来,如果我更改表的名称,那么这不会产生编译错误...

我的错误是这样的:

The specified type member 'Roles' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

下面的解决方案允许我现在编写代码:

proxy.User.Include(u => u.Role).First(u => u.UserId == userId)

这更好!

I'm currently learning a bit more about Linq-To-Entities - particularly at the moment about eager and lazy loading.

proxy.User.Include("Role").First(u => u.UserId == userId)

This is supposed to load the User, along with any roles that user has. I have a problem, but I also have a question. It's just a simple model created to learn about L2E

I was under the impression that this was designed to make things strongly type - so why do I have to write "Role"? It seems that if I changed the name of the table, then this wouldn't create a compilation error...

My error is this:

The specified type member 'Roles' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

The solution below allows me to now write the code:

proxy.User.Include(u => u.Role).First(u => u.UserId == userId)

Which is MUCH nicer!

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

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

发布评论

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

评论(1

初懵 2024-08-15 08:02:59
  1. Include 是对急切加载的提示,它不会强制急切加载。
  2. 在引用您希望通过 Include 急切加载的内容之前,请务必检查 IsLoaded 属性。
  3. 有多种方法可以将强类型对象放入 include 语句中,但是实体框架没有现成的解决方案可以解决此问题。谷歌类似:Entity Framework ObjectQueryExtension Include
  1. Include is a hint to eager load, it does not force eager loading.
  2. Always check the IsLoaded property before referencing something that you hope was eager loaded by Include.
  3. There are ways to put a strongly typed object in the include statement, but there is no solution available to this issue out of the box with Entity Framework. Google something like: Entity Framework ObjectQueryExtension Include
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文