Installation
Install AlmightyShogun.EntityFrameworkCore.ModelBuilding in the project that owns the DbContext, which is the project where OnModelCreating is written. The package targets net10.0.
dotnet add package AlmightyShogun.EntityFrameworkCore.ModelBuildingDependencies
Package references
Microsoft.EntityFrameworkCore10.0.11— suppliesModelBuilder, the type every helper extends.Microsoft.EntityFrameworkCore.Relational10.0.11— supplies the index filter behindApplyUniqueIndex.
Usage
The package registers no services and has no startup call. The helpers are extension methods on ModelBuilder, available inside OnModelCreating once the namespace is imported, and what one configures can still be extended afterwards with the standard fluent API on the same entity.
TIP
A helper call is model configuration like any other, so add a migration with dotnet ef migrations add <Name> after adding or changing one.
using Microsoft.EntityFrameworkCore;
using AlmightyShogun.EntityFrameworkCore.ModelBuilding;
public sealed class AppDbContext(
DbContextOptions<AppDbContext> options
) : DbContext(options)
{
public DbSet<Account> Accounts => Set<Account>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyOneToMany<Account, Order>(
account => account.Orders,
order => order.AccountId
);
modelBuilder.ApplyUniqueIndex<Account>(
account => account.Email
);
}
}Provider support
Only one thing here is relational: the filter argument on ApplyUniqueIndex, which is raw SQL whose identifier quoting differs per provider. That argument is why the package takes the relational dependency; everything else uses provider-agnostic Entity Framework Core APIs.
On a document provider such as Cosmos, how far these relationship shapes apply is that provider's own business. The helpers do no provider branching of their own.