在PostgreSQL中使用EF core3进行选择/删除/插入/更新等数据库操作之前如何拦截?
在EF Core3.0 https://docs.microsoft.com/en-gb/ef/core/what-is-new/ef-core-3.0/#interception-of-database-operations的新特性中,使用了DbCommandInterceptor。但是,我找不到这个类,也找不到AddInterceptors。
我是否需要安装新的nuget或使用特定的命名空间?
发布于 2019-10-11 06:30:27
我已经找到了我需要的东西,并成功地实现了。
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());
}
}
}https://stackoverflow.com/questions/58331311
复制相似问题