"Include" 在 Entity Framework Core 中 SelectMany + Select 之后不工作
"Include" not working after SelectMany + Select in Entity Framework Core
我使用 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; }
}
我需要做什么才能使返回的可查询中的翻译加载?
这是 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.
据此,您需要从context.Set<TitleInformation>()
开始查询。但是为了产生所需的过滤,您需要从 TitleInformation
到 Book
的反向导航 属性,您的模型目前缺少该导航:
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));
或者,如果TitleInformation
和Book
之间的关系是一对多的(上面是一对一的):
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));
我使用 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; }
}
我需要做什么才能使返回的可查询中的翻译加载?
这是 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.
据此,您需要从context.Set<TitleInformation>()
开始查询。但是为了产生所需的过滤,您需要从 TitleInformation
到 Book
的反向导航 属性,您的模型目前缺少该导航:
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));
或者,如果TitleInformation
和Book
之间的关系是一对多的(上面是一对一的):
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));