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.Orders.IncludeFilter(x => x.Items.Where(y => !y.IsSoftDeleted)
foreach(var order in list)
Console.WriteLine("Active invoice items in {0}: {1}", order.Name, order.Items.Count());
public static void GenerateData()
using (var context = new EntityContext())
context.Orders.Add(new Order() { Name = "Order_A", Date = DateTime.Now.AddDays(-4), IsSoftDeleted = true });
context.Orders.Add(new Order() { Name = "Order_B", Date = DateTime.Now.AddDays(-5), IsSoftDeleted = true });
context.Items.Add(new InvoiceItem() { Date = DateTime.Now.AddDays(-4), IsSoftDeleted = false, Price = 35, OrderId = 1 });
context.Items.Add(new InvoiceItem() { Date = DateTime.Now.AddDays(-4), IsSoftDeleted = false, Price = 22, OrderId = 1 });
context.Items.Add(new InvoiceItem() { Date = DateTime.Now.AddDays(-5), IsSoftDeleted = false, Price = 37, OrderId = 2 });
context.Items.Add(new InvoiceItem() { Date = DateTime.Now.AddDays(-5), IsSoftDeleted = true, Price = 7, OrderId = 2 });
public class EntityContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));
base.OnConfiguring(optionsBuilder);
public DbSet<Order> Orders { get; set; }
public DbSet<InvoiceItem> Items { get; set; }
public int OrderId { get; set; }
public string Name { get; set; }
public DateTime Date { get; set; }
public bool IsSoftDeleted { get; set; }
public List<InvoiceItem> Items { get; set; }
public int InvoiceItemId { get; set; }
public DateTime Date { get; set; }
public bool IsSoftDeleted { get; set; }
public int Price { get; set; }
public int OrderId { get; set; }