如何拦截EF core 3中的数据库操作?
How to intercept database operation in EF core 3?
如何在 PostgreSQL 中使用 EF core 3 select/delete/insert/update 等数据库操作之前进行拦截?
在EF core 3.0的新特性https://docs.microsoft.com/en-gb/ef/core/what-is-new/ef-core-3.0/#interception-of-database-operations中,使用了DbCommandInterceptor
。但是,我找不到这个 class 和 AddInterceptors
.
我需要安装新的 nuget 还是使用特定的命名空间?
我找到了我需要的并成功实施了。
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());
}
}
}
如何在 PostgreSQL 中使用 EF core 3 select/delete/insert/update 等数据库操作之前进行拦截?
在EF core 3.0的新特性https://docs.microsoft.com/en-gb/ef/core/what-is-new/ef-core-3.0/#interception-of-database-operations中,使用了DbCommandInterceptor
。但是,我找不到这个 class 和 AddInterceptors
.
我需要安装新的 nuget 还是使用特定的命名空间?
我找到了我需要的并成功实施了。
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());
}
}
}