using Z.EntityFramework.Plus;
using Microsoft.EntityFrameworkCore;
using Microsoft.Data.SqlClient;
using System.Collections.Generic;
public static void Main()
using (var context = new EntityContext())
context.Database.EnsureCreated();
using (var context = new EntityContext())
var list = context.Blogs.IncludeFilter(x => x.Posts.Where(y => !y.IsSoftDeleted))
.IncludeFilter(x => x.Posts.Where(y => !y.IsSoftDeleted)
.SelectMany(y => y.Comments.Where(z => !z.IsSoftDeleted)))
foreach (var blog in list)
Console.WriteLine("Active posts in {0}: {1}", blog.Title, blog.Posts.Count());
foreach (var post in blog.Posts)
Console.WriteLine("\t {0} : Active comments ({1})", post.Content, post.Comments.Count);
public static void GenerateData()
using (var context = new EntityContext())
context.Blogs.Add(new Blog() { Title = "Blog_A", IsSoftDeleted = true });
context.Blogs.Add(new Blog() { Title = "Blog_B", IsSoftDeleted = true });
context.Posts.Add(new Post() { Content = "Post 1 in Blog_A", Date = DateTime.Now.AddDays(-4), IsSoftDeleted = false, Price = 35, BlogId = 1 });
context.Posts.Add(new Post() { Content = "Post 2 in Blog_A", Date = DateTime.Now.AddDays(-4), IsSoftDeleted = true, Price = 22, BlogId = 1 });
context.Posts.Add(new Post() { Content = "Post 1 in Blog_B", Date = DateTime.Now.AddDays(-5), IsSoftDeleted = false, Price = 37, BlogId = 2 });
context.Posts.Add(new Post() { Content = "Post 2 in Blog_B", Date = DateTime.Now.AddDays(-5), IsSoftDeleted = false, Price = 7, BlogId = 2 });
context.Comments.Add(new Comment() { Text = "Comment 1 in post 1", IsSoftDeleted = false, PostId = 1 });
context.Comments.Add(new Comment() { Text = "Comment 2 in post 1", IsSoftDeleted = false, PostId = 1 });
context.Comments.Add(new Comment() { Text = "Comment 1 in post 3", IsSoftDeleted = true, PostId = 3 });
context.Comments.Add(new Comment() { Text = "Comment 2 in post 3", IsSoftDeleted = false, PostId = 3 });
public class EntityContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));
base.OnConfiguring(optionsBuilder);
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<Comment> Comments { get; set; }
public int BlogId { get; set; }
public string Title { get; set; }
public bool IsSoftDeleted { get; set; }
public List<Post> Posts { get; set; }
public int PostId { get; set; }
public string Content { get; set; }
public DateTime Date { get; set; }
public bool IsSoftDeleted { get; set; }
public int Price { get; set; }
public int BlogId { get; set; }
public List<Comment> Comments { get; set; }
public int CommentId { get; set; }
public string Text { get; set; }
public bool IsSoftDeleted { get; set; }
public int PostId { get; set; }