Path: redb/redb.Examples/ExamplesEf/E182_DeleteBatch_ef.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Examples/ExamplesEf/E182_DeleteBatch_ef.cs
----
using System.Diagnostics;
using Microsoft.EntityFrameworkCore;
using redb.Core.Models.Entities;
using redb.Examples.Ef;
using redb.Examples.Examples;
using redb.Examples.Output;
using Biz = redb.Examples.Models;
namespace redb.Examples.ExamplesEf;
///
/// EF Core twin of : five employees saved, loaded as whole objects, deleted in one
/// statement (the cascade removes their Props rows) and looked up again.
///
[ExampleMeta("E182_ef", "EF: Delete - Batch", "Core", ExampleTier.Free, 1, "EF", "Delete")]
public class E182_DeleteBatch_ef : EfExampleBase
{
public override async Task RunEfAsync(IDbContextFactory contexts)
{
var sw = Stopwatch.StartNew();
var batchId = DateTime.UtcNow.Ticks;
var headers = Enumerable.Range(0, 5).Select(i => EmployeeEfMapping.ToEntity(new RedbObject
{
name = $"BatchDelete_{batchId}_{i}",
Props = new Biz.EmployeeProps
{
FirstName = $"Temp{i}",
LastName = "Employee",
Age = 20 + i,
Salary = 40000m + i * 1000,
Department = "Test"
}
})).ToList();
await using (var db = await contexts.CreateDbContextAsync())
{
db.objects.AddRange(headers);
await db.SaveChangesAsync();
}
var ids = headers.Select(h => h._id).ToList();
int countBefore;
await using (var db = await contexts.CreateDbContextAsync())
countBefore = (await db.EmployeeProps.WithGraph().Where(e => ids.Contains(e._id)).ToBusinessListAsync()).Count;
await using (var db = await contexts.CreateDbContextAsync())
await db.objects.Where(o => ids.Contains(o._id)).ExecuteDeleteAsync();
int countAfter;
await using (var db = await contexts.CreateDbContextAsync())
countAfter = (await db.EmployeeProps.WithGraph().Where(e => ids.Contains(e._id)).ToBusinessListAsync()).Count;
sw.Stop();
return Ok("E182_ef", "EF: Delete - Batch", ExampleTier.Free, sw.ElapsedMilliseconds, ids.Count,
[$"Created: {ids.Count} objects",
$"Before delete: {countBefore}",
$"After delete: {countAfter}"]);
}
}