Path: redb/redb.Examples/ExamplesEf/E165_BaseFieldsBatch_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/E165_BaseFieldsBatch_ef.cs
----
using System.Diagnostics;
using Microsoft.EntityFrameworkCore;
using redb.Examples.Ef;
using redb.Examples.Ef.Entities;
using redb.Examples.Examples;
using redb.Examples.Output;
namespace redb.Examples.ExamplesEf;
/// EF Core twin of : ten header rows without Props, saved and loaded as a batch.
[ExampleMeta("E165_ef", "EF: Base Fields - Batch Operations", "Core", ExampleTier.Free, 1, "EF", "Base", "Batch")]
public class E165_BaseFieldsBatch_ef : EfExampleBase
{
public override async Task RunEfAsync(IDbContextFactory contexts)
{
var sw = Stopwatch.StartNew();
var now = DateTime.UtcNow;
var batch = Enumerable.Range(0, 1000).Select(i => new objects
{
_id_scheme = EfSchemeIds.None,
_date_create = now,
_date_modify = now,
_name = $"Batch_BaseFields_{now.Ticks}_{i}",
_value_string = $"Item_{i}",
_value_long = i * 100,
_value_bool = i % 2 == 0
}).ToList();
await using (var db = await contexts.CreateDbContextAsync())
{
db.objects.AddRange(batch);
await db.SaveChangesAsync();
}
var ids = batch.Select(o => o._id).ToList();
List loaded;
await using (var db = await contexts.CreateDbContextAsync())
loaded = await db.objects.Where(o => ids.Contains(o._id)).ToListAsync();
sw.Stop();
if (loaded.Count != 1000)
return Fail("E165_ef", "EF: Base Fields - Batch Operations", ExampleTier.Free, sw.ElapsedMilliseconds,
$"Expected 1000 objects, got {loaded.Count}.");
var verified = loaded.All(x => x._value_string != null && x._value_string.StartsWith("Item_"));
var result = Ok("E165_ef", "EF: Base Fields - Batch Operations", ExampleTier.Free, sw.ElapsedMilliseconds, loaded.Count,
[$"Saved and loaded: {loaded.Count} objects",
$"IDs: [{string.Join(", ", ids.Take(3))}...]",
$"Data verified: {verified}"]);
await using (var db = await contexts.CreateDbContextAsync())
await db.objects.Where(o => ids.Contains(o._id)).ExecuteDeleteAsync();
return result;
}
}