私はQuestionCategoriesと呼ばれる性質を持っている質問と呼ばれるエンティティを、持っている私のプロジェクトでは、変数のネストレベルでのオブジェクトのネストされたエンティティをロードし、それが型QuestionCategoryのオブジェクトのコレクションです:Entity Frameworkのコア:
public class Question : AuditEntity
{
public long QuestionId { get; set; }
public string Text { get; set; }
public byte Complexity { get; set; }
public short QuestionTypeId { get; set; }
public QuestionType QuestionType { get; set; }
public ICollection<AnswerOption> AnswerOptions { get; set; } = new Collection<AnswerOption>();
public ICollection<QuestionTag> QuestionTags { get; set; } = new Collection<QuestionTag>();
public ICollection<QuestionCategory> QuestionCategories { get; set; } = new Collection<QuestionCategory>();
}
QuestionCategoryクラスには、カテゴリCategoryのプロパティCategoryがあります。
public class QuestionCategory : AuditEntity
{
public long QuestionId { get; set; }
public long CategoryId { get; set; }
public Question Question { get; set; }
public Category Category { get; set; }
}
public class Category : AuditEntity
{
public long CategoryId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public long? ParentCategoryId { get; set; }
public Category ParentCategory { get; set; }
public ICollection<Category> ChildCategories { get; set; } = new Collection<Category>();
public ICollection<QuestionCategory> CategoryQuestions { get; set; } = new Collection<QuestionCategory>();
}
各カテゴリには、カテゴリParentCategoryというプロパティのカテゴリがあります。だからここで私はカテゴリのツリーを持っていて、ブランチの長さが異なり、ユーザーによって定義されています。
だけルートカテゴリとその子カテゴリーが存在する場合、現在、私はこのように質問のコレクションを読み込む:
public class QuestionRepository : BaseRepository<Question>, IQuestionRepository
{
protected override IQueryable<Question> DbQuery => base.DbQuery
.Include(q => q.QuestionCategories).ThenInclude(qc => qc.Category).ThenInclude(c => c.ParentCategory);
しかし、別の入れ子のレベルがある場合は、上記の子カテゴリ、すなわち自分の子供を持って、私は別の.ThenInclude()ステートメントを追加する必要があります。
public class QuestionRepository : BaseRepository<Question>, IQuestionRepository
{
protected override IQueryable<Question> DbQuery => base.DbQuery
.Include(q => q.QuestionCategories).ThenInclude(qc => qc.Category).ThenInclude(c => c.ParentCategory).ThenInclude(c => c.ParentCategory);
のように...
問題はnestiのような量でありますレベルは質問ごとに異なり、数量は制限されません。だから私はどうにか各質問のカテゴリレベルの数量を計算し、各レベルの.ThenInclude()を呼び出す必要があります。
しかし、私はそれをどのように実装するか考えていません。どんな助けも高く評価されます。
ありがとうございます!
Chris、EFCでは動作しません(EFでは動作しません)。ツリー(テーブル)全体をロードすると、動作する(作業した)唯一の方法です。 –
Chris、追加のネストレベルに対してThenInclude()ステートメントを追加しないと、その親レベルのParentCategoryプロパティはその値にnullを持ちます。 – Timur