How do I intercept before the database operation like select/delete/insert/update using EF core 3 in PostgreSQL?
In the new feature of EF core 3.0 https://docs.microsoft.com/en-gb/ef/core/what-is-new/ef-core-3.0/#interception-of-database-operations, DbCommandInterceptor
is used for that. However, I couldn't find this class nor the AddInterceptors
.
Do I need to install new nuget or using specific namespace?
Entity Framework Core (EF Core) interceptors enable interception, modification, and/or suppression of EF Core operations. This includes low-level database operations such as executing a command, as well as higher-level operations, such as calls to SaveChanges.
Entity Framework Core supports Database-First approach via the Scaffold-DbContext command of Package Manager Console. This command scaffolds a DbContext and entity type classes for a specified database.
Entity Framework (EF) Core, Microsoft's object-to-database mapper library for . NET Framework, brings performance improvements for data updates in version 7, Microsoft claims. The performance of SaveChanges method in EF7 is up to 74% faster than in EF6, in some scenarios.
I have found what I need and successfully implemented.
using System.Data.Common;
using Microsoft.EntityFrameworkCore.Diagnostics;
public class RowLevelSecurityInterceptor : DbCommandInterceptor
{
public override InterceptionResult<DbDataReader> ReaderExecuting(
DbCommand command,
CommandEventData eventData,
InterceptionResult<DbDataReader> result)
{
// command.CommandText += " OPTION (OPTIMIZE FOR UNKNOWN)";
return result;
}
}
public class MyDbContext : DbContext
{
// previous codes
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseNpgsql(GetConnectionString())
.AddInterceptors(new RowLevelSecurityInterceptor());
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With