在 Entityframework 中包含子对象

发布于 2024-10-31 12:12:00 字数 324 浏览 0 评论 0原文

我想在 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 技术交流群。

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

发布评论

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

评论(3

时常饿 2024-11-07 12:12:00

Include 不适用于投影。而且它是不需要的。只需这样做:

var query = context.Persons
                   .Select(x => new PersonDto
                       {
                          Id = x.pkid, 
                          FirstName = x.FirstName,
                          Address = x.Address
                       });

这里有几点:

  • 没有在投影中直接访问 Include
  • Address,EF 将处理这个问题
  • 我使用 PersonDto 作为投影目标。 PersonDto 只有 IdFirstNameAddress
  • 您可以投影到自定义类型或匿名类型,但不能投影到实体类型(映射类型) - 它不起作用并且会引发异常。
  • 如果您想使用映射类型,则不能仅返回选定的标量列 - 所有列将始终被加载。只能有选择地加载导航属性。为了克服这个问题,人们有时使用 表拆分 但如果您可以将大实体划分为分离的实体,那么这就是有效的。在您的场景中仅使用投影。

Include doesn't work with projection. Moreover it is not needed. Just do this:

var query = context.Persons
                   .Select(x => new PersonDto
                       {
                          Id = x.pkid, 
                          FirstName = x.FirstName,
                          Address = x.Address
                       });

Few points here:

  • No Include
  • Address accessed directly in projection, EF will handle this
  • I'm using PersonDto as target of projection. PersonDto has just Id, FirstName and Address.
  • You can project to custom type or anonymous type but you cannot project to entity type (the mapped type) - it doesn't work and it throws exception.
  • If you want to use mapped type you can't return only selected scalar columns - all columns will always be loaded. Only navigation properties can be loaded selectively. To overcome this people sometimes use Table splitting but that is something which works if you can divide your big entity into disjunct entities. In your scenario use just projection.
老街孤人 2024-11-07 12:12:00

您不能在投影上使用 Include() ,请尝试以下操作:

Iquerable<Persons> persons = Context.Persons
                                    .Include("Address")
                                    .Select(x=> new persons{Pkid=x.pkid, FirstName=x.FirstName})
                                    .AsQuerable();

此外,您还存在命名冲突,您投影到类型 persons 并希望将结果保存在 IQueryable 名为 persons - 其中一个是错误的。您是否有理由需要投影?你可以这样做

Iquerable<Persons> persons = Context.Persons.Include("Address");

You cannot use Include() on a projection, try this:

Iquerable<Persons> persons = Context.Persons
                                    .Include("Address")
                                    .Select(x=> new persons{Pkid=x.pkid, FirstName=x.FirstName})
                                    .AsQuerable();

Also you have a naming conflict, you project to a type persons and want to hold the results in an IQueryable named persons - one of them is wrong. Is there a reason you need the projection at all? You could just do

Iquerable<Persons> persons = Context.Persons.Include("Address");
南薇 2024-11-07 12:12:00

首先:检查是否启用了延迟加载。启用它后我经历了不同的结果。我更喜欢禁用延迟加载。

第二:检查此语法:

result = (From person In context.Persons.Include("Address")).ToList();

PS:有用的 EF 提示和信息技巧: http://blogs.msdn .com/b/alexj/archive/2009/03/26/index-of-tips.aspx

更新:

包含不起作用,因为您在新创建的对象上使用它,不是上下文中可用的对象。您应该在创建新对象之前使用 Include。

检查这个:

result = (From person In context.Persons.Include("Address") Select New With {.FirstName = item.FirstName, .AddressValue = item.Address.Value}).ToList();

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:

result = (From person In context.Persons.Include("Address")).ToList();

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:

result = (From person In context.Persons.Include("Address") Select New With {.FirstName = item.FirstName, .AddressValue = item.Address.Value}).ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文