简体   繁体   English

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

[英]Include not working after SelectMany + Select in Entity Framework Core

I have this query using Entity Framework Core (v2), but the Include / ThenInclude don't work as I expected. 我有使用Entity Framework Core(v2)的查询,但Include / ThenInclude不能按我的预期工作。 This is the query: 这是查询:

 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);

The query works, but the titles I get come with Title set to null . 查询有效,但我得到的标题标题设置为null

Just for clarification the classes are these 只是为了澄清这些课程

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; }
}

What do I have to do to make the Translations load in the returned queryable? 我需要做什么才能在返回的可查询中加载Translations?

This is current EF Core limitation described in the Loading Related Data - Ignored includes : 这是加载相关数据中描述的当前EF Core限制- 忽略包括

If you change the query so that it no longer returns instances of the entity type that the query began with, then the include operators are ignored. 如果更改查询以使其不再返回查询开头的实体类型的实例,则忽略包含运算符。

According to that, you need to start the query from context.Set<TitleInformation>() . 根据这个,你需要从context.Set<TitleInformation>()开始查询。 But in order to produce the desired filtering, you'll need inverse navigation property from TitleInformation to Book which currently is missing in your model: 但是为了产生所需的过滤,您需要从TitleInformationBook反向导航属性,该属性当前在您的模型中缺失:

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

Once you have it, you could use something like this: 一旦你拥有它,你可以使用这样的东西:

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

Or, in case the relationship between TitleInformation and Book is one-to-many (the above is for one-to-one): 或者,如果TitleInformationBook之间的关系是一对多(以上是一对一):

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

and respectively: 分别为:

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM