Automatic change events for entity framework core.
- Configure once, no way to forget to track changes
- Tracks old and new state
- Tracks success and failure of
SaveChanges()
- Database provider agnostic
Install with dotnet cli
Add package
dotnet add package EntityFrameworkCore.ChangeEvents
After setup (see below), remember to run and apply migrations
dotnet ef migrations add ChangeEvents
dotnet ef database update
Minimal setup
public class SampleContext : DbContext
{
public SampleContext(DbContextOptions<SampleContext> options)
: base(options)
{
}
public DbSet<WeatherForecast> WeatherForecasts { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Add the change event interceptor.
optionsBuilder.UseChangeEvents();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Add change event model.
modelBuilder.AddChangeEvents<ChangeEvent>();
}
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Add the change event interceptor, exclude TypeToExclude from change tracking
optionsBuilder.UseChangeEvents(options =>
options.ExclusionFilter = type => type == typeof(TypeToExlude));
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Add the change event interceptor, customize JSON serializer
optionsBuilder.UseChangeEvents(options =>
options.JsonSerializerOptions.Converters.Insert(0, new JsonStringEnumConverter()));
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Add change event model, create index on IsPublished
modelBuilder.AddChangeEvents<ChangeEvent>(e =>
e.HasIndex(x => x.IsPublished));
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Omit primary and foreign keys from change events
optionsBuilder.UseChangeEvents(options =>
{
options.OmitPrimaryKeys = true;
options.OmitForeignKeys = true;
});
}
Clone the project
[email protected]:mkromkamp/efcore-change-events.git
Navigate to the example project
cd example/EntityFrameworkCore.ChangeEvents.Examples.Api
Bring up docker-compose stack
docker-compose pull
docker-compose up -d --build
Apply migrations
dotnet ef database update
Start the example project
dotnet run