EF Core Integration Overview
The Weasel.EntityFrameworkCore NuGet package bridges Entity Framework Core's DbContext model to Weasel's schema management infrastructure. This allows you to use Weasel's migration tooling, delta detection, and CLI commands with schemas defined through EF Core's fluent API.
Installation
dotnet add package Weasel.EntityFrameworkCoreThe package depends on Weasel.Core and Microsoft.EntityFrameworkCore.Relational. You still need a database-specific Weasel package (e.g., Weasel.Postgresql or Weasel.SqlServer) for the Migrator implementation.
Use Cases
Marten + EF Core side by side -- When your application uses Marten for document storage and EF Core for relational entities, both systems can share Weasel's schema management. Marten already uses Weasel internally, so adding the EF Core integration unifies all schema migrations under one tool.
Polecat + EF Core -- In event sourcing architectures using Polecat, you can define read model projections with EF Core while Weasel manages all schema migrations across both the event store and the read models.
Standalone with Weasel CLI -- Use Weasel's command-line tools to generate migration scripts, assert schema validity, or apply changes for schemas defined entirely through EF Core's DbContext configuration.
Key Types
The integration is centered on a single static class and one record type:
DbContextExtensions-- Static extension methods that convert EF Core metadata into Weasel schema objects. This is the primary API surface.DbContextMigration-- A record wrapping aDbConnection,Migrator, andSchemaMigrationthat can detect and apply schema changes.
Quick Example
// Register a Migrator in DI (e.g., PostgresqlMigrator or SqlServerMigrator)
var services = new ServiceCollection();
services.AddSingleton<Migrator>(new PostgresqlMigrator());
// Later, create a migration from a DbContext
await using var migration = await serviceProvider.CreateMigrationAsync(dbContext, ct);
if (migration.Migration.Difference != SchemaPatchDifference.None)
{
await migration.ExecuteAsync(AutoCreate.CreateOrUpdate, ct);
}How It Works
- EF Core's
IModelmetadata is read from theDbContext(using the design-time model for full annotation access). - Each
IEntityTypeis converted to a WeaselITableviaMigrator.MapToTable(), preserving columns, primary keys, foreign keys, indexes, and JSON column mappings. - Entity types are topologically sorted by foreign key dependencies so referenced tables are created first.
- The resulting tables feed into Weasel's standard
SchemaMigrationpipeline for delta detection and DDL generation.
