The new EF Core interceptors

Interceptors

EF Core 7 has made a lot of enhancements to interceptors, You can see the list from EF Core improved interceptors.

  • Interception for creating and populating new entity instances (aka "materialization")
  • Interception to modify the LINQ expression tree before a query is compiled
  • Interception for optimistic concurrency handling (DbUpdateConcurrencyException)
  • Interception for connections before checking if the connection string has been set
  • Interception for when EF Core has finished consuming a result set, but before that result set is closed
  • Interception for the creation of a DbConnection by EF Core
  • Interception for DbCommand after it has been initialized

Lazy initialization of connection string

You generally don't need to use this feature, ABP has its own connection string feature.

The framework will automatically handle the module or multi-tenant connection string

Add interceptors in AbpDbContext

Add interceptors is very simple, Add your interceptors in the OnConfiguring method of DbContext

public class BookStoreDbContext : AbpDbContext<BookStoreDbContext>,
{

    public BookStoreDbContext(DbContextOptions<BookStoreDbContext> options)
        : base(options)
    {

    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.AddInterceptors(new MyEfCorenterceptor());

        base.OnConfiguring(optionsBuilder);
    }
}

Some interceptors may be Singleton services. This means a single instance is used by many DbContext instances. The implementation must be thread-safe.

See the EF Core Interceptors documentation for more information.