在 Entityframework 中包含子对象
我想在 IQueryable 列表中包含子对象。
我想在某些表类型 IQueryable 列表的选定列上包含子对象。
我尝试如下:
IQueryable<Persons> persons = Context.Persons.Select(x=> new persons{Pkid=x.pkid, FirstName=x.FirstName}).AsQueryable();
persons= persons.Include("Address");
包含子对象不起作用。任何人请帮忙。 .我哪里做错了.. 多谢...
I want to include child objects on an IQueryable list..
I want to include a child object on selected columns of some table type IQueryable list..
I tried like this:
IQueryable<Persons> persons = Context.Persons.Select(x=> new persons{Pkid=x.pkid, FirstName=x.FirstName}).AsQueryable();
persons= persons.Include("Address");
this include of child objects is not working..anyone please help...where I am doing wrong..
thanks alot...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Include
不适用于投影。而且它是不需要的。只需这样做:这里有几点:
Include
Address
,EF 将处理这个问题PersonDto
作为投影目标。PersonDto
只有Id
、FirstName
和Address
。Include
doesn't work with projection. Moreover it is not needed. Just do this:Few points here:
Include
Address
accessed directly in projection, EF will handle thisPersonDto
as target of projection.PersonDto
has justId
,FirstName
andAddress
.您不能在投影上使用
Include()
,请尝试以下操作:此外,您还存在命名冲突,您投影到类型
persons
并希望将结果保存在IQueryable
名为persons
- 其中一个是错误的。您是否有理由需要投影?你可以这样做You cannot use
Include()
on a projection, try this:Also you have a naming conflict, you project to a type
persons
and want to hold the results in anIQueryable
namedpersons
- one of them is wrong. Is there a reason you need the projection at all? You could just do首先:检查是否启用了延迟加载。启用它后我经历了不同的结果。我更喜欢禁用延迟加载。
第二:检查此语法:
PS:有用的 EF 提示和信息技巧: http://blogs.msdn .com/b/alexj/archive/2009/03/26/index-of-tips.aspx
更新:
包含不起作用,因为您在新创建的对象上使用它,不是上下文中可用的对象。您应该在创建新对象之前使用 Include。
检查这个:
First: Check if lazy loading is enabled or not. I experienced different results when it was enabled. I prefer lazy loading being disabled.
Second: Check this syntax:
P.S.: Useful EF Tips & Tricks : http://blogs.msdn.com/b/alexj/archive/2009/03/26/index-of-tips.aspx
UPDATE:
Include is not working, because your are using it on newly created objects, not the objects available in the context. you should use Include before creating new objects.
Check This: