Part of DKNet.EfCore.Extensions. Discover IDataSeedingConfiguration implementations and run them through EF Core native seeding callbacks.
Use the console-project setup in the Quick Start, then replace Program.cs with this example.
using Microsoft.EntityFrameworkCore;
using DKNet.EfCore.Extensions.Configurations;
using DKNet.EfCore.Extensions.Extensions;
using var connection = new Microsoft.Data.Sqlite.SqliteConnection("Data Source=:memory:");
connection.Open();
var o = new DbContextOptionsBuilder<DemoDb>();
o.UseSqlite(connection).UseAutoDataSeeding([typeof(DemoDb).Assembly]);
await using var db = new DemoDb(o.Options);
await db.Database.EnsureCreatedAsync();
await db.Database.EnsureCreatedAsync();
Console.WriteLine(await db.Items.CountAsync()); // 1
public sealed class DemoDb(DbContextOptions<DemoDb> options) : DbContext(options)
{
public DbSet<Item> Items => Set<Item>();
}
public sealed class ItemSeed : DataSeedingConfiguration<Item>
{
protected override ValueTask<ICollection<Item>> GetDataAsync(CancellationToken cancellation = default)
=> ValueTask.FromResult<ICollection<Item>>([new Item { Id = 7, Name = "Coffee" }]);
}
public sealed class Item
{
public int Id { get; set; }
public string Name { get; set; } = "";
}
DataSeedingConfiguration<TEntity> loads existing primary-key projections, including composite and shadow keys. Explicit non-default keys are compared by values, independent of entity equality. Empty candidates or no missing rows cause no save.
| Option | Type | Default | Effect |
|---|---|---|---|
assemblies |
Assembly[] |
required | Scan seed configurations; an empty array finds none. Null throws. |
Order |
int |
0 |
Metadata only; discovery does not sort it. |
EntityType |
Type |
typeof(TEntity) |
Exposes the targeted CLR type. |
SeedAsync |
nullable callback on interface | base implementation | Base loads GetDataAsync and inserts missing keys. Null callbacks are skipped. |
Order is declared but not sorted or otherwise read by the discovery runner. Combine dependent operations in one seeder.CancellationToken.None; the async callback forwards its cancellation token.