【发布时间】:2018-05-11 18:34:45
【问题描述】:
我使用 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; }
}
我必须做些什么才能使返回的可查询的翻译加载?
【问题讨论】:
-
你能显示实体的配置吗?
-
听起来像Ignored includes。您应该以其他方式构建查询,从
context.TitleInformation开始。 -
@H.Herzl 请检查更新 OP。
-
@IvanStoev 我怎么知道如何开始?你能包括代码吗?我有点失落:(
标签: c# entity-framework entity-framework-core iqueryable