2017-01-10 7 views
1

EFコアでは、.Includeおよび.ThenIncludeメソッドを使用して、関連するデータをクエリに読み込むことができます。のは、公式ドキュメントからの例を見てみましょう:上記の例で結果にマルチレベルナビゲーションプロパティを含むEntityFrameworkコア

1. using (var context = new BloggingContext()) 
2. { 
3.  var blogs = context.Blogs 
4.   .Include(blog => blog.Posts) 
5.   .ThenInclude(post => post.Author) 
6.   .ThenInclude(author => author.Photo) 
7.   .Include(blog => blog.Owner) 
8.   .ThenInclude(owner => owner.Photo) 
9.   .ToList(); 
10.} 

、それはPost.Authorプロパティが含まれ、その後、ライン5と6

ThenIncludeを使用してAuthor.Photoプロパティしかし、どのような場合Postエンティティが持っています私が含める別のナビゲーションプロパティ?私がThenIncludeを行6の後に使用すると、それはPhotoプロパティとの相対的なものになり、Includeを使用すると、Blogsプロパティに相対的に戻ります。クエリステートメントでこれを直接解決する方法はありますか?

+1

私は常にちょうど '.INCLUDE(X => x.SomeProperty.PropertyA).INCLUDE(X => X .SomeProperty.PropertyB).Include(x => x.SomeCollection.Select(y => y.PropertyC)) 'などです(しかし、コアで許可されているかどうかは6xです) – Will

答えて

1

あなたはできるだけ頻繁にあなたが好きな(と賢明と考える)と同じInclude Sを繰り返すことができます:

var blogs = context.Blogs 
    .Include(blog => blog.Posts) 
     .ThenInclude(post => post.Author) 
     .ThenInclude(author => author.Photo) 
    .Include(blog => blog.Posts) 
     .ThenInclude(post => post.Document) 
    .Include(blog => blog.Posts) 
     .ThenInclude(post => post. ...) 
関連する問題