Linq-To-Entities包含 [英] Linq-To-Entities Include

查看:99
本文介绍了Linq-To-Entities包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习更多关于Linq-To-Entities的内容,特别是关于急切和懒惰加载的时刻。

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

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



我的印象是,这是为了强化类型的设计,所以为什么我必须写角色 ?似乎如果我更改了表的名称,那么这不会创建一个编译错误...



我的错误是这样的:

  LINQ to Entities不支持指定的类型成员角色。只支持初始化器,实体成员和实体导航属性。 






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

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

哪个更好!


  • 总是检查IsLoaded属性在引用你希望被Include加载的东西之前。

  • 有一些方法可以在include语句中放置一个强类型的对象,但没有解决方案可用于此问题开箱即用与实体框架。 Google的内容如下:Entity Framework ObjectQueryExtension Include


  • 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!

    解决方案

    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

    这篇关于Linq-To-Entities包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆