在Entity Framework Core中选择SelectMany + Select之后不能正常工作

时间:2017-11-28 10:29:57

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

我使用Entity Framework Core(v2)进行此查询,但Include / ThenInclude无法正常工作。这是查询:

 var titlesOwnedByUser = context.Users
                   .Where(u => u.UserId == userId)
                   .SelectMany(u => u.OwnedBooks)
                   .Select(b => b.TitleInformation)
                   .Include(ti => ti.Title)
                   .ThenInclude(n => n.Translations);

查询有效,但我得到的标题标题设置为null

只是为了澄清这些类是

class User 
{
     public int Id { get; set; }
     public List<BookUser> OwnedBooks { get; set; }
}

class Book 
{
    public int Id { get; set; }
    public TitleInformation TitleInformation { get; set; }
    public List<BookUser> Owners { get; set; }
}

class BookUser 
{
     public int BookId { get; set; }
     public int UserId { get; set; }
     public Book Book { get; set; }
     public User User { get; set; }
}

class MyContext
{
     protected override void OnModelCreating(ModelBuilder modelBuilder)
     {
        modelBuilder.Entity<BookUser>()
            .HasOne(x => x.User)
            .WithMany(x => x.OwnedBooks)
            .HasForeignKey(x => x.UserId);

        modelBuilder.Entity<BookUser>()
            .HasOne(x => x.Book)
            .WithMany(x => x.Owners)
            .HasForeignKey(x => x.BookId);
     }
}

class TitleInformation
{
    public int Id { get; set; }
    public Title Title { get; set; }
    public Title Subtitle { get; set; }
}

class Title
{
     public int Id { get; set; }
     public string OriginalTitle { get; set; }
     public List<Translation> Translations { get; set; }
}

如何在返回的可查询中加载翻译?

1 个答案:

答案 0 :(得分:6)

这是Loading Related Data - Ignored includes中描述的当前EF Core限制:

  

如果更改查询以使其不再返回查询开头的实体类型的实例,则忽略包含运算符。

根据这一点,您需要从context.Set<TitleInformation>()开始查询。但是为了产生所需的过滤,您需要从TitleInformationBook的反向导航属性,该属性目前在您的模型中缺失:

class TitleInformation
{
    // ...
    public Book Book { get; set; } // add this and map it properly with fluent API
}

一旦你拥有它,你可以使用这样的东西:

var titlesOwnedByUser = context.Set<TitleInformation>()
    .Include(ti => ti.Title)
        .ThenInclude(n => n.Translations)
    .Where(ti => ti.Book.Owners.Any(bu => bu.UserId == userId));

或者,如果TitleInformationBook之间的关系是一对多(以上是一对一):

class TitleInformation
{
    // ...
    public List<Book> Books { get; set; }
}

和分别:

var titlesOwnedByUser = context.Set<TitleInformation>()
    .Include(ti => ti.Title)
        .ThenInclude(n => n.Translations)
    .Where(ti => ti.Books.SelectMany(b => b.Owners).Any(bu => bu.UserId == userId));