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.Posts.IncludeFilter(x => x.Comments.Where(y => y.RoleId >= myRoleID))
foreach (var post in list)
Console.WriteLine("{0} : Active comments ({1})", post.Content, post.Comments.Count);
public static void GenerateData()
using (var context = new EntityContext())
context.Roles.Add(new Role() { Name = "Administrator" });
context.Roles.Add(new Role() { Name = "HR" });
context.Roles.Add(new Role() { Name = "User" });
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, RoleId = 1 });
context.Comments.Add(new Comment() { Text = "Comment 2 in post 1", IsSoftDeleted = false, PostId = 1, RoleId = 3 });
context.Comments.Add(new Comment() { Text = "Comment 3 in post 1", IsSoftDeleted = false, PostId = 2, RoleId = 0 });
context.Comments.Add(new Comment() { Text = "Comment 1 in post 3", IsSoftDeleted = false, PostId = 3, RoleId = 3 });
context.Comments.Add(new Comment() { Text = "Comment 2 in post 3", IsSoftDeleted = false, PostId = 4, RoleId = 1 });
context.Comments.Add(new Comment() { Text = "Comment 3 in post 3", IsSoftDeleted = false, PostId = 4, RoleId = 1 });
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 DbSet<Role> Roles { 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 int RoleId { 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; }
public int RoleId { get; set; }
public int RoleId { get; set; }
public string Name { get; set; }