【问题标题】:Entity Framework Core 2 querying multi level collections with thenicludeEntity Framework Core 2 使用 theniclude 查询多级集合
【发布时间】:2018-09-13 00:37:40
【问题描述】:

我创建了两个类:

public class A {
  public int Id { get; set; }
  public ICollection<B> Bs { get; set; }
}

public class B {
  public ICollection<C> C1s { get; set; }
  public ICollection<C> C2s { get; set; }
}

然后我尝试使用 ThenInclude 方法获取它们:

var result = context.As          //public DbSet<A> As { get; set; }
  .Include(a => a.Bs)
    .ThenInclude(b => b.C1s)
  .Include(a => a.Bs)
    .ThenInclude(b => b.C2s)
  .SingleOrDefaultAsync(a => a.Id.Equals(id)); //id is given

return await result;

但不幸的是,C1s 和 C2s 集合都是空的。

如何检索与B一相关的C实体?

【问题讨论】:

  • 但是执行await命令后C1s和C2s都是空的。我将编辑一个帖子并在代码中添加 await 以显示它的位置,但我认为这无关紧要。

标签: include entity-framework-core .net-core-2.0


【解决方案1】:

我用

替换了 .ThenInclude() 方法
.Include("Points.MasterPoints")
.Include("Points.SlavePoints")

这解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 2014-05-21
    相关资源
    最近更新 更多