【问题标题】:How to include a child object's child object in Entity Framework 5如何在 Entity Framework 5 中包含子对象的子对象
【发布时间】:2019-05-16 07:07:24
【问题描述】:

我正在使用Entity Framework 5 code firstASP.NET MVC 3

我正在努力让子对象的子对象填充。下面是我的课..

应用类;

public class Application
{
     // Partial list of properties

     public virtual ICollection<Child> Children { get; set; }
}

儿童班:

public class Child
{
     // Partial list of properties

     public int ChildRelationshipTypeId { get; set; }

     public virtual ChildRelationshipType ChildRelationshipType { get; set; }
}

ChildRelationshipType 类:

public class ChildRelationshipType
{
     public int Id { get; set; }

     public string Name { get; set; }
}

存储库中的部分GetAll方法返回所有应用程序:

return DatabaseContext.Applications
     .Include("Children");

Child 类包含对 ChildRelationshipType 类的引用。要与应用程序的孩子一起工作,我会有这样的事情:

foreach (Child child in application.Children)
{
     string childName = child.ChildRelationshipType.Name;
}

我在这里得到一个错误,对象上下文已经关闭。

我如何指定每个子对象必须包含 ChildRelationshipType 对象,就像我在上面所做的那样?

【问题讨论】:

标签: entity-framework entity-framework-4 entity-framework-5 entity-framework-4.1


【解决方案1】:

如果您包含库 System.Data.Entity,您可以使用 Include() 方法的重载,该方法采用 lambda 表达式而不是字符串。然后,您可以使用 Linq 表达式而不是 string 路径来 Select() 覆盖子级。

return DatabaseContext.Applications
     .Include(a => a.Children.Select(c => c.ChildRelationshipType));

【讨论】:

  • 正如 GraemeMiller 所说,强类型类比使用字符串更易于维护
  • lba 方法是哪个版本可用的?我被困在 EF 4.0 代码库上......并且无法让 lamdas 工作。感谢您的任何意见。
  • 它将在 EF 4 中工作,只需确保添加对 System.Data.Entity; 的引用
  • 仅供参考 - 在 EF 6 中,命名空间是 Microsoft.Data.Entity
  • 使用 EF 5,我无法获得 .Select(x => x.Child) 但这有效 - Entities.UserProfile storedProfile = db.UserProfiles .Include(s => s.ShippingAddress) .Include (st => st.ShippingAddress.StateProvince) .Include(b => b.BillingAddress) .Include(bs => bs.BillingAddress.StateProvince) .FirstOrDefault(x => x.UserId == userId);
【解决方案2】:

使用 .NET Core 中的 EF Core,您可以使用关键字 ThenInclude

return DatabaseContext.Applications
 .Include(a => a.Children).ThenInclude(c => c.ChildRelationshipType);

包括儿童收藏中的儿童:

return DatabaseContext.Applications
 .Include(a => a.Childrens).ThenInclude(cs => cs.ChildRelationshipType1)
 .Include(a => a.Childrens).ThenInclude(cs => cs.ChildRelationshipType2);

【讨论】:

  • 谢谢!!!真的很有帮助!有时你可能会得到错误的智能感知,忽略它并构建! :)
  • 不错,我正在寻找.net core :)
  • 如果 Children 是一个集合,我需要在它下面包含属性怎么办?
  • 发现了一个超载。一开始并不明显。
  • @dodbrian 什么是过载?我正在尝试嵌套.ThenInclude,其中孩子是一个集合。
【解决方案3】:

我最终做了以下事情并且它有效:

return DatabaseContext.Applications
     .Include("Children.ChildRelationshipType");

【讨论】:

  • 强类型的方式更好。魔术字符串不适合重构
【解决方案4】:

使用通用存储库模式并为此实现通用解决方案的一个很好的例子可能看起来像这样。

public IList<TEntity> Get<TParamater>(IList<Expression<Func<TEntity, TParamater>>> includeProperties)

{

    foreach (var include in includeProperties)
     {

        query = query.Include(include);
     }

        return query.ToList();
}

【讨论】:

  • 如何调用上述方法?你能举个例子吗
  • @Jamee -List>> includers = new List>>(); includers.Add(x => x.FirstName);获取(包括);
  • 查询来自...?
  • 对不起@DougBeard,没有关注你的问题。
  • @gcoleman0828 上面代码 sn-p 中的查询变量。它被神奇地实例化了吗?它来自哪里?
猜你喜欢
  • 1970-01-01
  • 2021-11-08
  • 2017-07-13
  • 1970-01-01
  • 1970-01-01
  • 2011-08-02
  • 1970-01-01
  • 2021-07-01
  • 1970-01-01
相关资源
最近更新 更多