在Entity Framework Core中包含集合

时间:2016-04-13 14:17:21

标签: c# entity-framework entity-framework-core

例如,我有这些实体:

public class Book
{
    [Key]
    public string BookId { get; set; }
    public List<BookPage> Pages { get; set; }
    public string Text { get; set; }
} 

public class BookPage
{
    [Key]
    public string BookPageId { get; set; }
    public PageTitle PageTitle { get; set; }
    public int Number { get; set; }
}

public class PageTitle
{
    [Key]
    public string PageTitleId { get; set; }
    public string Title { get; set; }
}

如果我只知道BookId,我应该如何加载所有PageTit?

这就是我尝试这样做的方式:

using (var dbContext = new BookContext())
{
    var bookPages = dbContext
        .Book
        .Include(x => x.Pages)
        .ThenInclude(x => x.Select(y => y.PageTitle))
        .SingleOrDefault(x => x.BookId == "some example id")
        .Pages
        .Select(x => x.PageTitle)
        .ToList();
}

但问题是,它会抛出异常

  

ArgumentException:属性表达式&#39; x =&gt; {来自页面y   在x中选择[y] .PageTitle}&#39;无效。表达应该代表   财产访问:&#39; t =&gt; t.MyProperty&#39 ;.指定多个时   属性使用匿名类型:&#39; t =&gt;新{t.MyProperty1,   t.MyProperty2}&#39;。参数名称:propertyAccessExpression

出了什么问题,我该怎么办?

0 个答案:

没有答案