Skip to content

Automated change events for Entity Framework Core

License

Notifications You must be signed in to change notification settings

mkromkamp/efcore-change-events

Repository files navigation

EntityFrameworkCore.ChangeEvents

MIT License GitHub release (latest SemVer) GitHub issues GitHub Workflow Status (branch) GitHub Workflow Status (branch) GitHub Workflow Status (branch)

Automatic change events for entity framework core.

Features

  • Configure once, no way to forget to track changes
  • Tracks old and new state
  • Tracks success and failure of SaveChanges()
  • Database provider agnostic

Installation

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

Usage/Examples

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>();
    }
}

Exclude types from being tracked

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));
}

Customize JSON serializer

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    // Add the change event interceptor, customize JSON serializer
    optionsBuilder.UseChangeEvents(options => 
        options.JsonSerializerOptions.Converters.Insert(0, new JsonStringEnumConverter()));
}

Customize Entity framework model

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    // Add change event model, create index on IsPublished
    modelBuilder.AddChangeEvents<ChangeEvent>(e => 
        e.HasIndex(x => x.IsPublished));
}

Omit (foreign) keys from change events

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    // Omit primary and foreign keys from change events
    optionsBuilder.UseChangeEvents(options => 
    {
        options.OmitPrimaryKeys = true;
        options.OmitForeignKeys = true;
    });
}

Run sample project locally

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

Navigate to the swagger documentation

License

MIT